VBA sub with inputs

vba pass variable from one sub to another
vba call function from sub
excel vba call sub from another module
vba passing multiple arguments to function
vba sub return value
vba subroutine
vba function arguments
vba call variable from another sub

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

Calling Sub and Function procedures (VBA), Set up your sub something like this: Sub do_something(param As Integer) MsgBox "The parameter passed to this Sub is " & param End Sub. VBA SUB Step 1: In the Developer tab, click on Visual Basic in the Code group or you can use Step 2: Now, you can create a blank module i.e. Step 3: Start with subprocedure as follows and create code of VBA_SUB. Step 4: Once you start typing range , its argument appears in the parenthesis and

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://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/inputstatement

Sub statement (VBA), Arguments. VBA procedures can be passed data via arguments, which are declared in the procedure definition. For example, you could have a VBA Sub  Example. This example uses the Sub statement to define the name, arguments, and code that form the body of a Sub procedure. ' Sub procedure definition. ' Sub procedure with two arguments. Sub SubComputeArea(Length, TheWidth) Dim Area As Double ' Declare local variable. If Length = 0 Or TheWidth = 0 Then ' If

With a single argument, you don't use parenthesis

do_something 1

VBA sub with inputs, The difference between a function and a sub in Excel VBA is that a function can Explanation: This function has two arguments (of type Double) and a return  Excel VBA calling sub from another sub with multiple inputs, outputs of different sizes. I would like to call a sub from another sub inside in the same module. The first sub would be my main code and there I would call the second subroutine. Second subroutine receives multiple inputs as integer, double, double arrays and double matrices.

The complete Guide to Excel VBA Sub and how to use it, When a variable is passed to a subroutine or function it is called an argument. The value used in place of the parameter when we make the procedure call is called  Call Sub procedures with more than one argument. The following example shows two ways to call a Sub procedure with more than one argument. The second time it is called, parentheses are required around the arguments because the Call statement is used. Sub Main() HouseCalc 99800, 43100 Call HouseCalc(380950,

VBA Function and Sub Procedures, Sub CopyCol() 'VBA Calling Procedure Sub UseVar(ByVal Col as Integer) '​VBA Called Procedure An important point is the arguments type must match. The VBA Sub is an essential component of the VBA language. You can also create functions which are very similar to subs. They are both procedures where you write your code. However, there are differences and these are important to understand.

Excel VBA Function and Sub, We know that you could declare a global variable outside of any procedure. When using Here is an example of a sub procedure that takes two arguments: The main difference between a VBA Function procedure and a Sub procedure is that a Function procedure returns a result, whereas a Sub procedure does not. Therefore, if you wish to perform a task that returns a result (e.g. summing of a group of numbers), you will generally use a Function procedure,

Comments
  • Welcome to SO. What do you mean by another way?
  • According to how and where from you are calling the macro, there is different possibilities. See stackoverflow.com/questions/29858176/… and support.microsoft.com/en-gb/help/153307/… for example