Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA module subroutine not being picked up with parameter

Tags:

excel

vba

In a new excel spreadsheet, I have inserted a new module and entered a public sub. When trying to test, the sub does not appear in the macro list unless I remove the parameter.

This behaviour is bizarre and I cannot find any reference to it as an issue, everything I have read declares that subroutines (or functions, tried that too) can have parameters.

Public Sub RetrieveSIR()  <-- Can be found
Public Sub RetrieveSIR( SIRNumber as Integer)  <-- Cannot be found

It is driving me around the bend trying to work this out. If anyone can help, it would be much appreciated.

like image 248
Roddy Avatar asked Jan 31 '14 02:01

Roddy


People also ask

How do you pass parameters in VBA sub?

VBA allows you to pass variables into subroutines and functions in two ways. You can specify either ByVal or ByRef for each of the variables that are passed in. The ByVal and ByRef distinction for subroutine and function parameters is very important to make. In VBA all objects are passed by reference.

Can VBA subs take arguments?

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.

How do you call a subroutine in Excel VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses. Use a Sub procedure to organize other procedures so they are easier to understand and debug.

How do you call a subroutine from a different module?

To call a macro or function that is in the same workbook (it need not be in the same module) just type the name of the macro/function and any arguments it requires on one line of the calling macro. Another way is to prefix the macro/function called with the word Call.


1 Answers

Subs with parameters won't show up in the macro list for the same reason you can't simply run a sub with parameters from the vba editor screen. They can only be called via code so the parameter required can be input.

Edit: If for whatever reason you really need your macro to be in that macro list, you should make that parameter a variable in your macro and use an inputbox to specify it. That way when the user clicks the macro, they'll be prompted for the input and then the macro can run accordingly.

With regards to functions, you can have a function with parameters and use it as a formula in excel but as far as I'm aware they won't show up in the macro list either.

like image 95
Silenxor Avatar answered Nov 15 '22 00:11

Silenxor