Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Run-Time 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. 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.

Reply
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
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
Conditional Formatting - Run Time Error '13' Type Mismatch Error ksp Excel Programming 0 July 11th 06 07:06 AM
run-time error '1004': Application-defined or object-deifined error [email protected] Excel Programming 5 August 10th 05 09:39 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 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 11:29 AM.

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

About Us

"It's about Microsoft Excel"