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

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.

Related

RTE5 using `Shell` in VBA

Background:
Recently, a macro I use via VBA (in Excel) to run a script has stopped working; this subroutine worked for the previous six years. This is on a work device in which I do not have full admin privileges. The script file is stored on a network drive which I have access to, and can open and double-click to execute.
I have read from multiple sources, each not having an answer (including here on SO), that people have begun to see RTE5 when using Shell(), which may be related to security settings from the administrator.
One suggestion was to use use ShellExecute, which I have not had luck using.
Issue:
I receive RTE5 (invalid procedure call or argument) on the line for Shell() within the below code.
Question:
Has anyone had this same issue and been able to resolve said issue? Please indicate how you resolved.
Code:
'To open file, which previously worked:
Dim Loc As String
Loc = "Z:/filename.bat"
Call Shell(Loc, 1)
'Attempt at using ShellExecute, which gives Network Access error
Dim Loc As String
Loc = "Z:/filename.bat"
Call CreateObject("Shell.Application").ShellExecute(Loc, 1)

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.

Receiving Run time error 91 on only some Win 10 machines here

This is a weird error. It only occurs on some machines here, but not all of them. Furthermore, I researched this website and there are no solutions that I can see that cover this.
I receive this error when attempting to run some VBA code in an excel document.
The line of code it occurs in is in the
Private Sub UserForm_Initialize()
event.
The line of code is:
Set objTest = CreateObject("MTRClassLibrary.MTRTestComClass")
As I mentioned previously, the code only errors out on some of the machines here but not all of them.
It is not clear where and how the objTest instance is declared in the code. Also, you need to make sure the corresponding object is registered on the problematic machine. The CreateObject can't locate applications with ProgId passed as a parameter. Try to check the Windows registry for the string passed.
See Object variable not set (Error 91) for all possible cases.

Runtime error 5: Invalid procedure call or arguement

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

Injecting module into excel and run it

In VBScript I'm injecting a module into excel files and then I want to run them. The injection goes fine but when I run it says it can't find it. I've put the location in the trust center so it should trust it just fine. The module has a public sub named Run as well.
Dim XL
Set XL = CreateObject("Excel.Application")
Dim book
Set book = XL.Workbooks.Open(wkpath + "\" + wkname, 0, false)
book.VBProject.VBComponents.Import "C:\MyModule.bas"
XL.Application.Run("'" + wkname + "'!Run")
The paths and names all work out. Am I doing something wrong with this? What are my next debugging steps here.
[EDIT]
Actually it looks like some references aren't being selected now so it's getting an error about user-defined type not defined, but that's not the error that I get from VBScript. I had to do what is happening manually and then I saw that error.
So I was missing a reference but the error was pretty bogus. I figured this out by stopping my process once the excel file was open and adding in the module manually and trying to run the function. So I added the following in vbscript and it worked:
book.VBProject.References.AddFromFile "path to my xlam that has the type I use"

Resources