- I still have no idea how LastRowNumber gets ever value since the code is more than expected to fail when you try to invoke method of not initialized object variable CurrentSheet (see my previous post), unless it is initialized with the code you haven't posted.
- Check in object browser (F2 button in VBA IDE) what method "Add" or "Create" PivotCaches class has and what are the arguments of this method.
- What happen if you just remove "Version" argument from the problematic code line?
Create Pivot Table using VBA (Access 2007)
Collapse
X
-
-
If you need to find the last row number of the active sheet, there is no need to use CountA to do so. Like Fish I can't see why you don't have a run-time error occurring on the CountA line as a result of the uninitialised CurrentSheet object reference.
To find the last row number of the active sheet you can use
Code:LastRowNumber = AppExcel.ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row
StewartComment
-
From the VBA IDE (Alt-F11 from the main app window), use Ctrl-G to go to the Immediate Pane, and type :
Code:?xlPivotTableVersion12
Comment
-
.SpecialCells(x lLastCell) is really useful in Excel worksheets, but there are circumstances where it's unreliable (When rows or columns have been deleted since the last save for instance).
A technique I use to get the last Row or Column of a particular column or row (NB. Only works when you know which column or row to look in) is to go to the last position and skip back.
EG. If I know that column A has a unique identifier in it (or PK), then I use :
Code:Dim lngLastRow As Long lngLastRow = Range("A65536").End(xlUp).Row
Code:Dim intLastCol As Integer intLastCol = Range("IV1").End(xlToLeft).Column
Comment
-
So, reference it or use numeric equivalent of excel constants.
Oh, sorry, I see you've already tried. Did you replace all constants?
P.S. Anyway, I strongly recommend you to reference excel library in Access project. It will add Excel constants to Access namespace and enable Intellisense to resolve excel object model.Comment
-
That's an Office 2007 constant I see.
I tested in Access 2003 with a database with an Excel reference and saw nothing. When I changed it to xlPivotTableVer sion10 it worked fine for me, so a reference to Excel should make that constant available to you, as Fish so rightly says.Comment
-
I've added Microsoft Excel 12.0 Object Library and Microsoft Office 12.0 Object Library into my References....a nd am still getting '448' errors on line 2.
The code currently looks like this:
Code:With AppExcel.ActiveWorkbook.PivotCaches With .Add(SourceType:=xlDatabase, SourceData:=Datasheet.Range("A1:H" & LastRowNumber), Version:=3) Set PTable = .CreatePivotTable(TableDestination:=CurrentSheet.Range("A6"), TableName:="AnalysisPivot", DefaultVersion:=3) End With End With
Comment
Comment