Runtime error 5: Invalid procedure call or arguement - excel

I am new to VBA coding and have just started working on them. I tried writing a code to count the number of selected items in a slicer, but while compiling I got the following error:
Runtime error 5 - invalid procedure call or argument.
I have highlighted the line in which I am getting the error. Can someone please help me with the error?
I have tried using similar declarations for myslicer variable in other workbooks for different purposes and it works in those files.

First make sure that the ActiveWorkbook is the correct one.
ThisWorkbook.Activate

Related

vba application.unique works in one environment whilst not in the other

I have the following line of vba code in a subroutine:
filter_values_Lrow = Range("C"& Rows.count).End(xlUp).Row
contracts_products = Application.transpose(Application.WorksheetFunction.Unique(Range("B2:B"&filter_values_Lrow)).
This code works well in the "normal environment", whilst when I run this code on my virtual machine it produces an 438 error: Object doesn't support this property or method. Does anyone know why this could be the case? The excel file is able to be opened and other mutations on that file are possible.
Kind regards.

How is AppleScriptTask called from Excel 365 VBA on Mac mini running Big Sur?

I am trying to run an AppleScript from VBA in an Excel 365 macro and I keep getting:
Run-time error '5': Invalid procedure call or argument
I have this script called "PythonCommand.scpt" in my /Library/Application Scripts/com.microsoft.Excel folder:
on PythonCommandHandler(pythonScript)
--do shell script "/usr/local/bin/Python3" & pythonScript
return "Handler ended! " & pythonScript
end PythonCommandHandler
I commented out the "do" statement so I should simply get back what I send it. I tested this in the script editor by adding a line to invoke the function and it works just fine.
I have this code in my VBA macro:
Dim result As String
Dim strPyScript As String
strPyScript = "xxxx"
result = AppleScriptTask("/Library/Application Scripts/com.microsoft.Excel/PythonCommand.scpt", "PythonCommandHandler", strPyScript)
and when I run it I get the error '5'.
I tried changing the first argument to just "PythonCommand.scpt" instead of the whole path but got the same error. I tried putting the last argument in as a quoted string instead of using a variable and got the same result. I have looked at this post:
How can I launch an external python process from Excel 365 VBA on OSX?
and started my coding from there (this example had the first argument with no path). Then I read this one:
https://learn.microsoft.com/en-us/office/vba/office-mac/applescripttask from Microsoft which is specific to using the AppleScriptTask command. It lays out the process a bit more clearly but is basically the same. I also looked at this post:
How to simply run an applescript task from mac excel 2016
which has a broken link to a Ron deBruin article which I found here:
https://macexcel.com/examples/setupinfo/applescripttask/index.html
Which is quite clear and easier to read but says basically the same thing. The post with the broken link was resolved by making the script an app and invoking it as a hyperlink. I tried that and it works but there are several shortcomings with that approach: can't pass an argument to the script, can't get anything back from the script, and control does not wait for the script to end before executing the next line of VBA code. I really want to make the AppleScriptTask command work. I feel I must be missing something. If others have gotten this to work I must be doing something wrong. I tried turning on all the references I could find in Tools References but that didn't change anything, I still got the error '5': message. Please help me out here if you can. I really appreciate any help you can offer.
Thanks
Phil
Phil,
The code I use is:
res = AppleScriptTask("selectFile.scpt", "GetFile", args)
NB I don't need to put the full path to the scpt file. I presume this is because it is sandboxed and so vba knows where it is.
BTW I put the scpt file in this folder:
${HOME}/Library/Containers/com.microsoft.Excel/Data/Library/Application Scripts/com.microsoft.Excel
The folder you have specified is not a user folder, but rather a system folder.

MS Office last updates causing some strange behavior in handling VBA code

Our admins ran some MS Office 2016 updates (Build 11929.20838) and all of a sudden I noticed a few unexpected errors in one of my previously written Excel VBA macros. For example below error is popping up against any variable which I have not defined explicitly. It was not the case before and unless I had mentioned Option Explicit, I had never experienced anything like this.
Compile error:
Can't find project or library
I am also getting the same error against the following 2 statements of my code, whereas it used to run smoothly before the last Microsoft Office 2016 update.
' Against the following two declarations, the compiler is highlighting
' Date in parenthesis and quoting same error "Can't find project or library"
xMonth = VBA.DateTime.Month(Date)
xYear = VBA.DateTime.Year(Date)
' And showing same error as above against defining an Array variable like below
srchString = Array("invoice_number", "invoice_date", "Category_wise_code", "Bill To Customer", "consignees_address_long")
'And even here, it is giving the same error:
[A1].Font.Bold = True
Does anyone have any idea about these errors and how to resolve this issue? All of this quite unexpected. Let me tell you this macro has been in my use for over 4 years and I never ran into issues like these.
You will find something fishy here:
https://www.google.com/search?rlz=1C1CHZL_enGB838GB838&ei=EOInX-ibPMeusAXFw7z4Cg&q=%22ms+office+2016%22+%22july+updates%22+%22VBA%22&oq=%22ms+office+2016%22+%22july+updates%22+%22VBA%22&gs_lcp=CgZwc3ktYWIQAzoECAAQRzoICCEQFhAdEB46BQghEKABUOTJCljq_gtgg4UMaABwAXgAgAHzAogByiiSAQYyLTIuMTSYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab&ved=0ahUKEwjo4_6J5f7qAhVHF6wKHcUhD68Q4dUDCAw&uact=5
Although not all links are opening, but Google window is briefly showing that some people are also facing macro crash situation.
Thanks for your time reading my post.

Cannot Run Visual Basic Editor Because of a Syntax error on Application.Calculate

I have a VBA Excel model which I have separated into two separate workbooks:
InputsWB, which contains all the inputs for the model, and
RunnerWB, which contains the bulk of the VBA code (and all the class modules live here).
Depending on the needs of the user, the InputsWB can either call the macro in the RunnerWB or the RunnerWB can call multiple versions of the InputsWB. The detail doesn't matter for this question but the two workbooks need to be separated.
Mysteriously, after the split I sometimes get a Run-time 1004 error with the message Cannot Run Visual Basic Editor Because of a Syntax error. If I Debug + F5 then the code runs until it encounters the next Application.Calculate.
I have seen many similar questions while recording macros, or on Application.Calculate. I think mine is related to the Application.Calculate, but those answers don't explain why this happens. Some reasons I can think off, but cannot confirm online is:
There are Modules in the two workbooks with the same names (although I've changed the modules names in the InputsWB but still get the error on occasion.
There are VBA functions in the two workbooks with the same names
Are there any other reasons why I could be getting these errors?
UPDATE (2019/12/02)
I finally realised why I got this error. I am calling a macro in my RunnerWB from my InputsWB. However, there was a duplicate function declared in my RunnerWB. Usually, this would give a Compile error: ambigious name detected when the macro was initiated from ThisWorkbook. However, because it was initiated from another workbook I got the Syntax error explained above.
UPDATE (2020/07/22)
I am now getting this error again on Application.Calculate right at the end of the run. Which breaks me is that I use Application.Calculate many times during the run.
UPDATE (2020/07/23)
I found the issue that caused the bug at last.
I had a sub called Main in a Module called Main
One of the buttons in my workbook referred to this button, and because the names are the same it links it as Main!Main
Later I renamed the module to MainSub. The button still worked, but something inside VBA was broken which was triggered in some Application.Calculate executions.
Conclusion
Never give your sub the same name as your module in VBA, as this may cause problems down the line.

Runtime error 5

I am on Day one in learning Excel Macros. As per the tutorial I recorded the macros and tried to run it. (I have used the same file that they have provided in the tutorial and it is a .csv file)
But it showing me the following Error
Runtime Error
Invalid Procedure call or Argument
When I tried to debug it is showing as follows:
Need Help
-Thanks
You can set the CommandType property only if the value of the QueryType property for the query table or PivotTable cache is xlOLEDBQuery, which is enum 5.
This is not the case for your query. In fact, that line doesn't even exist in the source code. Perhaps you attempted to merge scripts? If so, be careful when merging connection code that the types are the same.
If you add in a debug line to get the QueryType
Debug.Print .QueryType
.Name = "receipts"
you will get the enum 6 back. You can look this up using the object explorer:
As would be expected from the connection specification:
ActiveSheet.QueryTables.Add(Connection:="TEXT;

Resources