Passing Arguments in Excel VBA

Home/Excel VBA/Passing Arguments in Excel VBA

We write the procedures to perform certain tasks and some times we may required to write another procedure with small variations. In this situation we can take advantage of the Passing Arguments.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


In this Section:

What are By Value (ByVal) and By Reference (ByRef) Passing Arguments?

We can pass the arguments in two different ways:
1. By Value (ByVal): We will pass the actual value to the arguments
2. By Reference (ByRef): We will pass the reference (address, pointers in other language) to the arguments

ByRef is default passing argument type in VBA. This means, if you are not specifying any type of the argument it will consider it as ByRef type. However, it is always a good practice to specify the ByRef even if it is not mandatory.

Writing a procedure or a function with Arguments:

Syntax for Passing Arguments:
'Procedure:
Sub ProcedureName(Arguments)
'Statements…
End Sub
'Function:
Function FunctionName(Arguments) As DataType
'Statements…
Function Sub

Following example shows how to write a Function to add two integers:
'Function to add two integers
Function fnSum(ByVal intVal1 As Integer, ByVal intVal2 As Integer) As Long
fnSum = intVal1 + intVal2
End Function

Following example shows how to write a Procedure to multiply two integers:
'Procedure to multiply two numbers
Sub sbMultiplyValues(ByVal intVal1 As Integer, ByVal intVal2 As Integer)
MsgBox intVal1 * intVal2
End Sub

How to call a function or a procedure?

  • We can call a function from either a procedure or function or Worksheet.
  • And we can call a procedure from either a procedure or function.
Calling a Function from a Procedure:
'Procedure to call function to add two values
Sub sbAddValues()
MsgBox fnSum(200, 300) ‘Here 200 and 300 are the parameters passing to the function (fnSum)
End Sub

Calling a Procedure from another Procedure:
'Procedure to call procedure to multiply two values
Sub sbCallMultiplyValues()
Call sbMultiplyValues(200, 300)
End Sub

Calling a Function from another Function:
'Function to add three integers
Function fnSumA(ByVal intVal1 As Integer, ByVal intVal2 As Integer, ByVal intVal3 As Integer) As Long
fnSumA = fnSum(200, 300) + intVal3
End Function

Calling a Function from Worksheet:

You call the user defined functions as similar to the built-in excel function. The following picture shows how to call a user defined function to add to add two integers:

Passing Arguments in Excel VBA – Example Macro File to Download

You can download the example file

[mongopono.ru – Passing Arguments.xlsm] and have a look into this.

LIMITED TIME OFFER
By |May 12th, 2013|Excel VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

2 Comments

  1. Sub December 29, 2016 at 4:37 PM - Reply

    I am trying to call the below Sub but getting a syntax error.
    Sub AddNewWorkbook1(ByVal nPas As String)

    ‘Adding New Workbook
    Workbooks.Add
    nPassword.Copy
    Range(a1).Select
    ActiveSheet.Paste
    ‘Saving the Workbook
    ActiveWorkbook.SaveAs “C:\WorkbookName.xls”

    End Sub

    ‘calling sub
    AddNewWorkbook1 (f)

    • Shane July 3, 2017 at 3:55 PM - Reply

      “A1”

Leave A Comment


Related pages


sample excel workbookactivex vbaddl command in sqlvba for applicationsconcatenate excelexcel vba refresh all pivot tablesmacro programingexcel macros examplescountif formula in excel 2007microsoft word meeting notes templatevba code to copy data from one sheet to anotherhow to label columns in excelshortcut key for deleting sheet in excelworksheet tabsturn on macros in excel 2007microsoft excel duplicate findervba templatesexcel formula to delete blank rowswhat is vlookup formula in exceldeveloper tab in excel 2013weekly gantt chart excelunprotect worksheet without passwordbutton vba excelunprotect excel forgot passwordmsgbox in vbaexcel insert shortcutms excel 2007 formulas and functionsunlock columns in excelunprotect worksheet excelvba run sql queryhow to send email using vbavlookup example datahow to lock a spreadsheetswot analysis ppt templatevba code to open excel filems excel hlookupchecking duplicates in exceluse of vlookup and hlookup in excelexcel filter table based on cell valuetask assignment template excelhow to save excel macroconditional macrosfile exists vbscripthow to turn on macro in excelinstr function in vbaexcel vba objectscount if function excelhow to add developer tab in excel 2013checkbox syntaxexcel if statement with two conditionsdatediff vba exceldeduplication in excelexcel yearly schedule templateexcel unhide first columnusing excel formulas in vbaestimate spreadsheet templatehow do i insert a row in excelintroduction to vbacommands of dbmsexcel how to change rows to columnsexcel transfer data to another sheetmicrosoft excel multiple if statementsselect case excel vbaexcel 2007 calendar templateexcel select rowsiserror excelnested ifs in exceliferror in vlookupuseful excel templatespower point vbahow to generate charts in excelms excel advanced tutorialexcel removing duplicatesclustered column chart in excel