I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".
Related
I have an Add-In installed in my Excel (which is stored in a separate folder) that I want to run in one of my Excel files through a VBA macro. Whenever I call the:
Application.Run "'Addintorun.xla'"
I get the error that the macro may not be available. I know I might need to enable all macros through Macro Security, but I can't tick this option since it's blocked by the administrator. However the add-in per se works fine, I just can't call it from the macro. I also added a reference to the Add-in in my module but nothing changed.
There are two ways to accomplish what you need:
Add the add-in as reference to the workbook you need calling its Subs, Function(s). In this way you may call them exactly as in the add-in itself. Only in case of the same macro names, the add-in name must be used when make a call.
You can call the Subs which are not private in the next way:
Application.Run "'Addintorun.xla'!MyMacro"
The simple quotes are necessary only if the add-in name contains spaces, but it is a good habit to use them all the time, since they do not bother a name without spaces, neither...
You can also obtain a result of a (not Private) function in the next way:
Dim res As Long
res = Application.Run("'Addintorun.xla'!MyFunction", 3, 9)
Debug.Print res
'MyFunction should receive two `Long` parameters and returning also a `Long`
'It may return any kind of variable, but res must be declared to match it.
Such subs/functions can also be called using the add-inn full name, instead of its name. In this way, the add-in will be loaded/open during the call, if not already loaded...
I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".
I imported a module (.bas) into my excel project.
However, when I click on the button Run, Excel ask me the macro name and even if I put the name of my macro, this is not working because the Execute button is disabled.
Someone can help me to fix it please ?
Thanks in advance.
EDIT 1
The macro name is : Sub calcul(heureOuverture As String, heureFermeture As String)
If I don't use parameters this is working...
1) Compile your project. This can be done by Debug > Compile VBA Project
2) Use Option Explicit at the top of each module.
3) Before you run the macro use the Break (CTRL + Break) and the Rest buttons to ensure clean slate.
4) If you need to pass arguments to a routine you cannot use the Run Sub/Userform (F5) from the VBE as you will need to stipulate the arguments. As such, you can consider something like the below:
Sub TestCall()
Call calcul("myStr1","myStr2")
End Sub
A sub-procedure (Macro) with parameters cannot run directly. It must be called from another macro with required parameter. You may do it like this:
Sub CallerMacro()
'calcul "heureOuverture value", "heureFermeture value"
Call calcul("heureOuverture value", "heureFermeture value")
End Sub
I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".
I want to execute a complex command-line command when I click a cell in excel, a hyperlink cell. I've tried everything but I can't get it to work.
You can right-click and choose Hyperlink but the dialog doesn't support complex arguments (arguments with double quotes). You simply get an error "Invalid link".
You can use the =HYPERLINK() formula but that doesn't support arguments at all. Only web URLs are executed, such as mailto: and callto:. URLs can have parameters passed with &arg=value but that's no good for command-line arguments.
I don't know what else to try, any ideas?
You need to pass a Shell command with the following syntax
e.g.
Sub RunCalculator()
Dim ret as Double 'Optional
ret = Shell("C:\Windows\System32\Calc.exe")
MsgBox(ret) ' in case you are interested in the return value
End Sub
To execute when clicking a cell:
You need to put your code in a macro that's invoked from the Worksheet_SelectionChange() event which you can get by using the dropdown in the VBE when you open the code for the worksheet in the editor.
Unrelated side note: The name reminds of the times when one used the DOS shell