Invalid procedure call or argument in excel - excel

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 .

Related

Refresh a sql connection file using a single button

I am trying to record a macro to refresh a sql connection file using a single button in vba but it comes up with an error. On hitting the refresh button to which I have assigned the macro, it says
Run time error 91 - You attempted to use an object variable that isn't yet referencing a valid object.
Sub RefreshCRS610()
'
' RefreshCRS610 Macro
'
'
With Selection.ListObject.QueryTable
.Connection = Array( _
"OLEDB;Provider=SQLOLEDB.1;Password=FDReport;Persist Security Info=True;User ID=FDReport;Initial Catalog=M6FDBGRP;Data Source=mlirvcb00" _
, _
"1;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FXV10084;Use Encryption for Data=False;Tag w" _
, "ith column collation when possible=False")
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT DISTINCT OKCONO As 'Company Number', OKCUNO As 'Customer Number', OKSTAT As 'Status', OKRESP As 'Branch Resp" _
, "onsible' FROM M6FDBGRP.MPXFDTA.OCUSMA")
.Refresh BackgroundQuery:=False
End With
End Sub
I was trying to automate refreshing the sql connection table without hitting right click-> edit query->table->okay-> enter pwd again and again. Trying to just refresh the azure sql database table by one click.
Run time error 91 - You attempted to use an object variable that isn't yet referencing a valid object.
Try getting the valid Selection object instance from the Application or ActiveWorksheet instances.

"Out of Memory" VBA error after trying to register descriptions of UDFs

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.

VBA Pivot Table error "Invalid procedure call or argument"

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.

VBA - Update Linked Table of an Access file from Excel

I am trying to change the linked table address from an Access file "Hey.accdb" using VBA coding from an Excel file.
I've coded the script below in my Excel file and it prompts the error "Object required" when I run it. Can someone please help me with this problem. I've been staring at it for too long. Thanks.
Sub RunMacroinAccesswithPara2()
Set Db = CreateObject("Access.Application")
Db.OpenCurrentDatabase "D:\Database1\Hey.accdb"
Db.Visible = True
Db.AutomationSecurity = msoAutomationSecurityLow
DoCmd.TransferDatabase TransferType:=acLink, _
DatabaseType:="Microsoft Access", _
DatabaseName:="V:\Reporting\Quarterly\2018Q2\JP\Data\04\Database\Valuation_Database.mdb", _
ObjectType:=acTable, _
Source:="Valuation_Database_Adjusted", _
Destination:="Valuation_Database_Adjusted"
End Sub
DoCmd belongs to the Access application object.
So use
Db.DoCmd.TransferDatabase ' etc.
Edit
To update the link, you need the TableDef object, set its .Connect property and run .RefreshLink.
See Linked table ms access 2010 change connection string

'DoCmd.TransferSpreadsheet' to export to Excel

I need to EXPORT an access table from ACCESS to EXCEL. There are 618499 rows. I tried simple method: right-click - export - excel. It worked only when I choose export with format (without format created blank excel document), but it is not convenient because I'll have to create 10 documents. I tried to use this code but it became red and did not work:
DoCmd.TransferSpreadsheet(
[acExport],
[acSpreadsheetTypeExcel12Xml],
[tab_1],
["C:\Users\admin\Desktop\import_2014\final.xlsx"],
[True])
What should I change in it?
How should I type it in a module.
Or maybe you can offer another simple solution.
All VBA code must be in a sub or function, that is in a module.
Get rid of all those [], and if you want to put each parameter into a new line, use the line concatenation symbol _
DoCmd.TransferSpreadsheet _
acExport, _
acSpreadsheetTypeExcel12Xml, _
"tab_1", _
"C:\Users\admin\Desktop\import_2014\final.xlsx", _
True
EDIT
Either use Call:
Call DoCmd.TransferSpreadsheet(acExport, acSpreadsheetTypeExcel12Xml, _
"tab_1", "C:\Users\admin\Desktop\import_2014\final.xlsx", True)
or omit the parentheses:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
"tab_1", "C:\Users\admin\Desktop\import_2014\final.xlsx", True

Resources