For example: Function Example (strValue) If (strValue = "A") Then Example = 1 Exit Function End If If (strValue = "B") Then . How to fix vlookup returning N/A value even though data was matched? VBA - Return a Value from a Sub Procedure In this Article Using Variables to Return a Value Global Variable Using a Sub Procedure to Populate an Excel Sheet This tutorial will teach you how to return a value from a sub procedure in VBA. VBA InStr Function | How to Use Excel VBA InStr Function? var = AddIt (1,2) Msgbox Var. To return a value using the Return statement Put a Return statement at the point where the procedure's task is completed. You can use "Exit Function" statements to make the function return with different values at different points. Function Method1 (FirstNumber As Long, LastNumber As Long) As List (Of Long) Dim map As New List (Of Integer) Dim n As Long For n = FirstNumber To LastNumber map.Add (n) Next Return map End Function The VBA code is similar except we swap the Return for a Set For example, you can pass two numbers in a function and then you can expect from the function to return their multiplication in your calling program. The macro writes multiple output arguments from the MATLAB workspace to Microsoft Excel cells.. To work with VBA code in Excel with Spreadsheet Link™, you must enable Spreadsheet Link . in Excel VBA: Dim rngActiveCell As . Step 1 - MsgBox with two lines of message Public Sub test1 () sInput = MsgBox ("Are you sure to delete the data?" & vbNewLine & "Data will be deleted permanently") End Sub Step 2 - Add OK / Cancel Button Public Sub test2 () sInput = MsgBox ("Are you sure to delete the data?" & vbNewLine & "Data will be deleted permanently", vbOKCancel ) End Sub Can be called from VBA\Button\Event etc. BlueDolphin. How to return multiple values from a VBA function (Part 1) Part 1: Using By Reference argument, collection, and dictionary object VBA functions (and other programming languages as well) can only return one value. Unio You'll need to use the SET keyword anytime you are assigning an object instead of a value: Sub mySub () dim myDict as Dictionary set myDict = myFunc () End Sub Function myFunc () as Dictionary dim myDict2 as Dictionary set myDict2 = new Dictionary 'some code that does things and adds to myDict2' set myFunc=myDict2 End Function Contribute to yasenstar/vba development by creating an account on GitHub. VBA - Split Function - Tutorials Point For example, A1:I46 is a block, A48:I93 is the second block, etc. Follow the Return keyword with an expression that yields the value you want to return to the calling code. Public sub test () x = 2 msgbox number (x) Exit Sub Public Function number (num) if len (num) = 1 then num = "0" + num End if End Function The function is returning a null value. AddIt = I + J. VBA - Return a Value from a Sub Procedure - Automate Excel Answers. Click to expand. Assign the return value the name of the function. A return value of 1 or 2 indicates that the function ran successfully, but with a condition. A function can only return one value. Function return value - Microsoft Access / VBA