LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Error in Excel VBA Code (Error 91)

I have written a section of code that extracts all data from a
worksheet & puts it into a new pivot table. The code works every other

time that I run it, which is strange. Every other time that I run it,
I get a run-time
error #91. I ran into a similar problem
some time ago that I was able to fix by setting object variables to
nothing at the end of my code, however that doesn't seem to work here.
Below is the code:

Dim xlWb As Object
Dim xlWs As Object
Dim xlAp As Object


Set xlWb = ActiveWorkbook
Set xlWs = xlWb.Sheets("Sheet3")


a = xlWs.Range("A1").Address
lastcell = xlWs.Range("A1").SpecialCells(xlCellTypeLastCell). Address
MyRange = xlWs.Name & "!" & a & ":" & lastcell


With xlWb
'Add Table & cross fingers
.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=MyRange) _
.CreatePivotTable TableDestination:="", TableName:="PivotTable1"
End With


'Move desired column headings to correct sections on pivot table report

With ActiveSheet.PivotTables("PivotTable1").PivotFields (strField)
.Orientation = xlRowField
.Position = 1
End With


With ActiveSheet.PivotTables("PivotTable1").PivotFields (strField2)
.Orientation = xlColumnField
.Position = 1
End With


With ActiveSheet.PivotTables("PivotTable1").PivotFields (strField3)
.Orientation = xlDataField
.Position = 1
.Function = xlSum
End With


'Close Pivot Table Menus
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False


'release excel objects after all activity
'******
Set xlWs = Nothing
Set xlWb = Nothing
Set objXl = Nothing
Set objSht = Nothing


....one other bit of info since I didn't include all of the code. The
variables 'strField, strField2, strField3' are defined as follows:


strField = objXl.Selection.Cells(1, 1).Text
strField2 = objXl.Selection.Cells(1, 7).Text
strField3 = objXl.Selection.Cells(1, 6).Text


....where 'objXl' is a New Excel.Application


I think this may be a problem area even though I'm setting it to =
nothing at the end of the code, but heck, I don't know. Any ideas as
to why this works exactly 1/2 of the time would be greatly appreciated.

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Excel 2007 code Lorne[_2_] Excel Discussion (Misc queries) 3 November 6th 07 05:39 PM
Code compiles but Excel ERROR Roba Excel Programming 2 May 6th 04 08:29 PM
How can I still go to the error-code after a On Error Goto? Michel[_3_] Excel Programming 2 May 4th 04 04:21 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM


All times are GMT +1. The time now is 01:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"