Please help me debug this code.
I am getting the following error:
"Invalid procedure call or argument"
Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MainSheet!R1C1:R" & lastRow & "C9", Version:=xPivotTableVersion15).CreatePivotTable TableDestination:="Pivot Sheet!R2C1", TableName:="Compile table", DefaultVersion:=xlPivotTableVersion15
xPivotTableVersion15 looks like a typo and
TableDestination:="'Pivot Sheet'!A1"
sheet names with spaces need to be enclosed in single quotes
I did a macro in Excel 2016 and a user on an older version of Excel got this error. In my code it had Version:=6 and DefaultVersion:=6. When I changed this to be xlPivotTableVersion14 it worked fine.
Related
Good day,
I'm trying to insert a pivot table from a table but also add it to a data model via VBA.
I've recorded the process manually and also followed the example of this link: https://powerpivotpro.com/2014/07/adding-tables-to-a-model-from-vba-in-excel-2013/
I change the variables for my data path and workbook name that has been set previously in the code. When I run the macro I get the following error on the first line: "Run-Time error 5: Invalid procedure call or argument".
I've never added a pivot table to a data model via VBA before, thus not sure how the code should look (except what I've seen online and mine seem to adhere to the example)
MainWB.Connections.Add2 "WorksheetConnection_" & MainWB.Name & "!Table_SDCdata", "", _
"WORKSHEET;" & DataPath & "\" & MainWB.Name & ", " & MainWB.Name & "!Table_SDCdata", 7, True, False
MainWB.PivotCaches.Create(SourceType:=xlExternal, SourceData:=MainWB.Connections("WorksheetConnection_" & MainWB.Name & "!Table_SDCdata" _
), Version:=6).CreatePivotTable TableDestination:=wsPivotModel.Name & "!R1C1", TableName:="PvtSDCmodel", DefaultVersion:=6
The variables are as follows: (** added to replace for security in this question, both variables are correct and used many times in the code without problems)
MainWB.name: **2791 SDC Inland 12.08.2020.xlsbCOMBINED.xls
DataPath: C:_Store\Simone_d_drive\Desktop*\2019\Macros*\raw data**\2791\2020.08.16
Any help or point in the right direction will be great thank you!
I managed to find my errors:
Updated code:
MainWB.Connections.Add2 "WorksheetConnection_" & MainWB.Name & "!Table_SDCdata", "", _
"WORKSHEET;" & DataPath & "\" & MainWB.Name, MainWB.Name & "!Table_SDCdata", 7, True, False
I had to remove 2 of the quotation marks, one at the end of the connection string and the 2nd one at the beginning of the command text.
I've a quick question for one of my vba codes , the code is basically consolidating 12 tables into one sheet called temp range B8. The problem is that if I run it using Excel 2016 English version it works fine on any computer but if the excel is 2016 spanish version , it will fail with error: " Execution Error 1004"
I've created 12 modules with macros and only one is failing using the spanish version for the following code
Range("B8").Consolidate Sources:=Array( _
"Enero[#All]", "Febrero[#All]", "Marzo[#All]", "Abril[#All]", "Mayo[#All]", "Junio[#All]", "Julio[#All]", "Agosto[#All]", "Septiembre[#All]", "Octubre[#All]", "Noviembre[#All]", "Diciembre[#All]" _
), Function:=xlCount, TopRow:=True, LeftColumn:=False, CreateLinks:=True
Changing the reference type from #All to #Todo was the fix , I-m good now
I'm glad you found a solution to your problem for the Italian version of Excel.
Perhaps, you could make some changes to your code so it could handle both cases (English and Italian). For instance, you could use the following function that returns the MsoLanguageID for Excel (inspired by this answer):
Function ExcelLanguage() As MsoLanguageID
ExcelLanguage = Application.LanguageSettings.LanguageID(msoLanguageIDUI)
End Function
You could then use an if-statement to handle both cases like this:
If ExcelLanguage = msoLanguageIDItalian Then
Range("B8").Consolidate Sources:=Array( _
"Enero[#Todo]", "Febrero[#Todo]", "Marzo[#Todo]", "Abril[#Todo]", "Mayo[#Todo]", "Junio[#Todo]", "Julio[#Todo]", "Agosto[#Todo]", "Septiembre[#Todo]", "Octubre[#Todo]", "Noviembre[#Todo]", "Diciembre[#Todo]" _
), Function:=xlCount, TopRow:=True, LeftColumn:=False, CreateLinks:=True
Else
Range("B8").Consolidate Sources:=Array( _
"Enero[#All]", "Febrero[#All]", "Marzo[#All]", "Abril[#All]", "Mayo[#All]", "Junio[#All]", "Julio[#All]", "Agosto[#All]", "Septiembre[#All]", "Octubre[#All]", "Noviembre[#All]", "Diciembre[#All]" _
), Function:=xlCount, TopRow:=True, LeftColumn:=False, CreateLinks:=True
End If
For all the MsoLanguageID values, see MSDN Documentation.
I have created a series of VBA mathematical functions in an Excel spreadsheet (i.e. minimisation algorithms). These functions have been tested and they seem to be working properly. I want to add a description of these function and their arguments therefore, based on this question on the topic, I tried to code some subroutines that would achieve that:
First, I created a subroutine to actually encapsulate the descriptions:
Sub RegisterUDF()
myFunctionOneDescription = "Long FunctionOne description" & vbLf _
& "myFunctionOne(<...>, ..., <...>)"
myFunctionOneArguments = Array("FunctionOne argument 1 description", _
"FunctionOne argument 2 description", _
"FunctionOne argument 3 description", _
"FunctionOne argument 4 description", _
"[Optional] FunctionOne argument 5 description")
myFunctionTwoDescription = "Long FunctionTwo description" & vbLf _
& "myFunctionTwo(<...>, ..., <...>)"
myFunctionTwoArguments = Array("FunctionTwo argument 1 description", _
"FunctionTwo argument 2 description", _
"FunctionTwo argument 3 description", _
"FunctionTwo argument 4 description", _
"[Optional] FunctionTwo argument 5 description")
myFunctionThreeDescription = "Long FunctionThree description" & vbLf _
& "myFunctionThree(<...>, ..., <...>)"
myFunctionThreeArguments = Array("FunctionThree argument 1 description", _
"FunctionThree argument 2 description", _
"FunctionThree argument 3 description", _
"FunctionThree argument 4 description", _
"[Optional] FunctionThree argument 5 description")
Application.MacroOptions Macro:="myFunctionOne", Description:=myFunctionOneDescription, ArgumentDescriptions:=myFunctionOneArguments, Category:=9
Application.MacroOptions Macro:="myFunctionTwo", Description:=myFunctionTwoDescription, ArgumentDescriptions:=myFunctionTwoArguments, Category:=9
Application.MacroOptions Macro:="myFunctionThree", Description:=myFunctionThreeDescription, ArgumentDescriptions:=myFunctionThreeArguments, Category:=9
End Sub
Then, I create the following subroutine in the ThisWorkbook object:
Private Sub Workbook_Open()
Call RegisterUDF
End Sub
so that the descriptions are automatically loaded when I open the workbook.
When creating these two subroutines and assessing the look of the function descriptions in the Function UI (namely the one that pops up when you press Ctrl+Shift+A or fx), I started closing and reopening the workbook given descriptions are updated only when Workbook_Open() is executed. Then, at some point I started getting an Out of Memory error immediately after opening the workbook; the error seemed to originate from the third function description above:
I started getting rid of these two subroutines but now I still see the Out of Memory error when I refresh my workbook (whose tabs are populated with instances of my user-defined functions); each time, the Out of Memory error seems to originate in one of my UDFs but not always the same. In addition, when I try to cancel the debugging by resetting VBA, I get a new Out of Memory error immediately after, before having refreshed the workbook or performed any additional action, thus I end trapped in a "loop" of Out of Memory errors and I am forced to close Excel from the Task Manager (1). This had never happened before I tried to code the functions' descriptions.
Can anybody help me in understanding what might be going on? I suspect this is related to the utilisation of Application.MacroOptions but I am unsure. Any help is greatly appreciated.
(1) I suspect this is actually explained by the fact that, when I refresh the workbook or a tab, multiple instances of my UDFs will try to reevaluate hence each Out of Memory error corresponds to one instance of my UDFs.
[EDIT #1] I observe very strange behaviour. For example, on a tab with preexisting instances of my UDFs in some cells, I have tried to evaluate one of my functions in a new cell after having commented out the 2 subroutines described above. When writing down the function in the cell and pressing enter, I got the Out of Memory error originating from the code of that UDF. Then, I deleted from the VBA code the commented subroutines and when pressing enter the function evaluated correctly! However, when refreshing that tab, I then got the same memory error but this time coming from another UDF.
This seems to have been solved by following the procedure outlined below(1):
Delete subroutines RegisterUDF() from module and Workbook_Open() from Workbook object;
Copy all the VBA code from the module(2) into e.g. the notepad;
Delete the workbook's module;
Create a new module in the same workbook;
Paste code into new module.
The Out of Memory errors have ceased after this manipulation, however I still do not know what the origin was in the first place, and I am not sure whether this could happen again if I try to reprogram RegisterUDF() and Workbook_Open(). Any additional explanation to this bug is greatly appreciated.
(1) Other tricks, such as copying the whole workbook; restarting the computer; copying/pasting the code to a new workbook without deleting the old module from the original workbook; etc. did not seem to solve the issue.
(2) All my code was located in a single module.
I am getting this error on command type=0 : Run-time error:'5'. Invalid procedure call or arguement in excel
this image is being shown when
I try to run the macro while doing automation in exce
I see at least Excel 2016 puts this code line .CommandType = 0 in the code while recording a macro while getting a QueryTable. But it is definitely wrong and fails while running that recorded macro later. So do removing it.
Instead the recorded:
With ActiveSheet.QueryTables.Add(Connection:= _
"...", Destination:=Range("$A$1"))
.CommandType = 0
.Name = "..."
...
use:
With ActiveSheet.QueryTables.Add(Connection:= _
"...", Destination:=Range("$A$1"))
' .CommandType = 0
.Name = "..."
...
Hint: Recording a macro is a good start. But knowledge about the used objects according to their documentation is also necessary. So always have a look at this documentations. In this case QueryTable.CommandType Property .
saw this question was posted earlier but the answer was a workaround using a macro filter. Still trying to understand how the "Autoshow Method" works from https://msdn.microsoft.com/en-us/library/office/ff835595.aspx
The code I'm working with is:
Sub Test()
ActiveSheet.PivotTables("Pivot4").PivotFields("Base Contract #") _
.AutoShow xlAutomatic, xlTop, 10, "Sum of Not Satisfactory"
End Sub
Runtime Error 1004 - Unable to get pivotTable properties of the worksheet class
What else do I need to declare to have the code work?
Thanks!
Art