How do I feed parameters into a sub? When I run a Macro, the sub just runs without asking for any input.
Sub do_something(input as integer)
xxxxxx
end sub
I know I can use the sub in another sub/function where I can give it a input. Like the following,
sub caller_function()
call do_something(1)
end sub
Is there another way to use do_something?
Thanks in advance!
Set up your sub something like this:
Sub do_something(param As Integer)
MsgBox "The parameter passed to this Sub is " & param
End Sub
To call it:
do_something 1
If you want more than one parameter..
Sub do_something(param1 As Integer, param2 As String)
MsgBox "The parameters passed to this Sub is " & param1 & " and " & param2
End Sub
To call it:
do_something 4,"Hello"
Or for a bit or reading chaos:
do_something param2:="Hello", param1:=4
The code that you provided does not compile. Maybe you were trying to provide a simple example...?
It does not compile because Input is a reserved word, used to read data from a file object. You cannot use it as a variable name. (I have assumed that "xxxxx" is indicating where code should go, i.e. 'Do something here.)
The following code works, but you cannot run it without passing a value:
Sub do_something(i As Integer)
MsgBox i
End Sub
If you want to make the variable optional, you can add the Optional keyword. Then it will run even if a value is not passed:
Sub do_something(Optional i As Integer)
MsgBox i
End Sub
You could also use a globally scoped variable, which would allow the sub to run without directly supplying a value:
Option Explicit
Public i As Integer
Sub do_something()
MsgBox i
End Sub
Regarding Input: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputstatement
With a single argument, you don't use parenthesis
do_something 1
Related
I'm new in VBA
I wrote a code like this:
sub a1
some codes
end sub
sub a2
some codes
end sub
when I run my code the only code that be executed is sub a1!
how can I run other subs?
If you have 2 procedures
Sub FirstProcedure()
'Some code
End Sub
Sub SecondProcedure()
'Some code
End Sub
and you want to run both of them sequencially, you just need to call them
Sub RunBoth()
FirstProcedure
SecondProcedure
'Some more code
End Sub
If you now run RunBoth it will call the FirstProcedure and after that has finished it will call SecondProcedure
If you just want to call one of the procedures from the VBA editor, place your cursor in one of the procedures and press Run at the toolbar or F5 on the keyboard.
Alternative to #PEH 's approach via Application.Run
This approach demonstrate possible usage via Application.Run. Similar to OP, I confined myself to procedure calls without passing arguments here.
A) Numbered procedure calls
If you'd dispose of numbered procedures with identical name prefix (e.g. "Proc1", "Proc2"),
you may code as follows:
Sub ExampleCall()
runNumberedProcedures "Proc", 2
End Sub
Sub runNumberedProcedures(ByVal ProcName As String, ByVal ProcNumber As Long)
Dim i As Long
For i = 1 To ProcNumber
Application.Run "Proc" & i
Next i
End Sub
Undocumented Caveat
Note that Run needs an additional module prefix for procedure names ranging from A to XFD (e.g. in the case of Module.a1 or Module1.a2),
as apparently VBA tries to avoid internal conflicts with Excel column names. Therefore a numbered procedure starting with "XFE" or "Proc" needn't be prefixed expressly.
Side note: Personally I'd prefer more meaningful naming conventions than a1,a2 or Proc1,... apart from testings.
B) Procedure calls via list of procedure names
If you want to run any procedure names in a given order, you might call a sub and pass a procedure list as argument (here: "Proc1,Proc2,Module1.a1"):
Sub ExampleCall2()
'[1]run listed procedures
runListedProcedures "Proc1,Proc2,Module1.a1"
'do other stuff
'...
'[2]run procedure a2 later
Run "Module1.a2" ' note module prefix for proc names from A to XFD !
End Sub
Sub runListedProcedures(ByVal ProcList As String)
Const Delim As String = ","
Dim procedures As Variant
procedures = Split(ProcList, Delim)
Dim i As Long
For i = LBound(procedures) To UBound(procedures)
Application.Run procedures(i)
Next i
End Sub
Can someone explain what am I doing wrong here. Excel doesn't seem to set the variable value if I run the second macro before the first one. I would like to set the values just once and use them in multiple macros. I have 6 macros that will be using the same values. The code included below is a short code I use to test.
Option Explicit
Dim Test As String
Sub sub1()
Test = "Test"
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
This is the popup i get if the second macro is executed before the first one.
Just call the second macro from the first. Otherwise Test will not be assigned a value.
You can read about scope here.
Dim Test As String
Sub sub1()
Test = "Test"
MsgBox Test
sub2 'or call sub2
End Sub
Or you can use a Constant, e.g.
Public Const Test As String = "Test"
(see my edit below - the original answer doesn't answer the question you asked properly)
You can set it as a public variable
Option Explicit
Public Test As String
Sub sub1()
Test = "Test"
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
Or call sub2 from sub1 with a parameter
Sub sub1()
Test = "Test"
MsgBox Test
'call the second sub with the Test variable here
sub2 Test
End Sub
Sub sub2(test as string)
MsgBox test
End Sub
or have Sub1 be a function and return the value
Function sub1()
Test = "Test"
MsgBox Test
'call the second sub with the Test variable here
sub1 = Test
End Function
Sub sub2()
test = sub1()
MsgBox test
End Sub
EDIT :
Or you could wrap it all in another sub so you are able to define the public variable
Public Test as string
Sub sub1()
MsgBox Test
End Sub
Sub sub2()
MsgBox Test
End Sub
Sub run()
Test = "Test"
sub1()
sub2()
End Sub
If you want a variable to keep its value for the whole excel session (i.e. a static variable) use the Global keyword
Global Test As String
Remember, one of your procedures must assign a value to it. Otherwise it will have a value of ""
I am trying to write a sub in VBA that looks like the following,
Public Sub Value(X As Double)
Code in here...
End Sub
However, whenever I try to run this, it doesn't recognize the Sub I just wrote. Why is this?
I can get the sub to work if I write it like,
Public Sub Value()
Dim X As Double
Code in here...
End Sub
However, I need to do it the first way. Is there something that I'm missing, that I need to include in my code to be able to write it the first way?
See this example:
Public Sub Value(X As Double)
MsgBox X + 1
End Sub
Public Sub Test()
Value 2.2
End Sub
Running Test will give you:
All,
I'm struggling with passing input variables from a userform to a module/sub. I've searched the form, but can't find a matching example.
E.g.
Form 1: Two input fields named TextBoxA and TextBoxB and 1 Submit button.
Module 1: some code in which I would like to use the input variables of TextBoxA and TextBoxB
Code in userform:
Private Sub CommandButton1_Click()
pass = myuserform.TextBoxA
End Sub
Who can point me in the right direction. Much appreciated.
Setup a Method which accepts two parameters in a Standard Module and pass the TextBox values as arguments:
Public Sub TestMethod(ByVal param1 As Variant, ByVal param2 As Variant)
MsgBox "You passed: " & param1 & " and " & param2
End Sub
Then you only need to call the method in your button's Click event:
Private Sub CommandButton1_Click()
TestMethod myuserform.TextBoxA.Value, myuserform.TextBoxB.Value
End Sub
I am wondering if there is a simpler way to "loop" a sub to do a number of times.
I would like to do the following:
Sub defined()
'code here to be looped
End sub
Sub use()
... 'previous codes
Call defined 'do defined once
... 'interlude codes
Call defined(3) 'do defined action thrice
... 'interlude codes
Call defined(2) 'do defined twice
End Sub
Is there something like that exist? Or I must use Loop, something I still am a little confused on how to code when applying this method with a call sub.
Thank you for your time.
edited for tags
You could do something like this:
Sub Defined(numCalls as Integer)
Dim i as Integer
If not numCalls > 0 Then Exit Sub
For i = 1 to numCalls
'The rest of your code
Next
End Sub
This will allow you to do:
Call defined(3)
Etc.
for i = 1 to 3
Defined '("Call" is optional)
next i