Excel VBA Sub procedures cannot return values directly like Function procedures, but you can still pass data back to the calling code using ByRef parameters or by modifying global variables. This guide shows you how to effectively return information from a Sub, which is essential for modular, reusable code that updates multiple cells, variables, or objects based on logic inside the Sub.

What You'll Need

  • Microsoft Excel with VBA enabled (Excel 2010 or later)
  • Access to the Visual Basic for Applications editor (press Alt+F11)
  • A basic understanding of VBA variables, data types, and procedure types (Sub vs Function)
  • A worksheet to test your code (even a blank one)

Step-by-Step

  1. Open Excel and press Alt+F11 to open the Visual Basic for Applications (VBA) editor.
  2. In the Project Explorer pane on the left, right-click your workbook name (e.g., VBAProject (YourWorkbook.xlsx)) and select Insert > Module. This creates a new code module.
  3. Define your Sub procedure with at least one ByRef parameter. For example, declare a Sub called UpdateResult that takes a variable by reference: Sub UpdateResult(ByRef result As String). The ByRef keyword means the actual variable in the calling code will be modified, not just a copy.
  4. Inside the Sub, assign a value to the ByRef parameter. For example: result = "Processed successfully". You can also modify multiple variables passed by reference, such as integers, dates, or ranges.
  5. Optionally, include logic like loops, conditionals, or worksheet interactions. For example, read a value from cell A1, process it, and set the result string accordingly.
  6. Close the Sub with End Sub.
  7. Now create a test Sub that calls your first Sub. In the same module, add: Sub TestReturn(). Inside it, declare a variable: Dim myResult As String.
  8. Call your first Sub, passing the variable: UpdateResult myResult. Note: do not use parentheses around myResult when calling a Sub with ByRef.
  9. After the call, use MsgBox myResult or Range("B1").Value = myResult to see the returned value.
  10. Press F5 while your cursor is inside TestReturn() to run it. Check cell B1 or the message box for the output.
  11. For returning multiple values, pass several ByRef parameters. Example: Sub GetData(ByRef name As String, ByRef score As Integer, ByRef dateRecorded As Date). Assign each inside the Sub and pass them all in the calling routine.
  12. To return a range reference, declare the parameter as Range: ByRef targetRange As Range. Then assign it in the Sub like Set targetRange = Range("C1:C5"). Always use Set for object variables.
  13. Save your workbook as a macro-enabled file (.xlsm) to preserve the VBA code.

Common Mistakes

  • Using ByVal instead of ByRef: If you pass a variable ByVal, changes inside the Sub won’t affect the original variable. Always use ByRef when you intend to return a value.
  • Forgetting Set for object variables: When assigning a Range, Worksheet, or Workbook to a ByRef parameter, you must use the Set keyword: Set myRange = Range("A1"). Omitting Set causes a runtime error.
  • Calling a Sub with parentheses: If you write UpdateResult(myResult) instead of UpdateResult myResult, VBA may interpret it as a Function call and throw an error, especially if the parameter is not a simple value type.
  • Not declaring the variable before calling: If you pass an undeclared variable, VBA may treat it as Variant, which can cause unexpected behavior or type mismatches.
  • Assuming Sub can return like Function: Trying to write UpdateResult = "value" inside a Sub will cause a compile error. Only Functions can return values this way.

Tips

  • Use ByRef parameters to return multiple values from a single Sub — this is cleaner than using global variables.
  • Always initialize your ByRef parameters before passing them (e.g., Dim result As String: result = "") to avoid Null or Empty values causing logic errors.
  • If you’re updating a range, pass the range object ByRef and modify its .Value or .Formula properties directly inside the Sub, rather than returning a string and reassigning it later.
  • For complex data structures (like arrays or collections), pass them ByRef to avoid copying large memory blocks — it’s faster and more efficient.
  • Use meaningful parameter names like outputValue, statusFlag, or errorDescription to make your code self-documenting.
  • Consider combining this approach with error handling: if the Sub encounters an issue, set a ByRef error variable to a descriptive string and check it after the call.

Troubleshooting

  • If you get a "Compile error: Invalid use of property", check that you’re not using parentheses around the parameter list when calling a Sub. Remove the parentheses.
  • If the variable doesn’t change after the Sub runs, confirm you used ByRef, not ByVal. Change the parameter declaration and recompile.
  • If you see "Object variable or With block variable not set", you tried to assign an object (like a Range) without using Set. Add Set before the assignment.
  • If the message box shows blank or 0, check that your Sub actually assigns a value to the ByRef parameter — verify logic paths (e.g., did an If statement skip the assignment?)
  • If Excel crashes or freezes, check for infinite loops or recursive calls. Add a debug.Print statement to trace execution flow.
  • If the code works in the editor but not when triggered from a button, ensure the button is linked to the calling Sub (TestReturn), not the UpdateResult Sub.
  • If you’re using Excel Online or a web version, VBA is not supported. This method only works in desktop Excel.

Frequently Asked Questions

Can a VBA Sub return a value like a Function?

No, a Sub cannot return a value directly. Use ByRef parameters to modify variables in the calling code instead. If you need a return value, convert the Sub to a Function.

Why use ByRef instead of just changing a global variable?

ByRef keeps your code modular and avoids side effects. Global variables can be accidentally modified anywhere in the project, making debugging harder. ByRef makes dependencies explicit and reusable.

Can I return an array from a Sub?

Yes. Declare the parameter as an array (e.g., ByRef arr() As Variant) and assign values to it inside the Sub. Use ReDim Preserve if you need to resize it dynamically.

What if I need to return a Boolean status?

Use a ByRef Boolean parameter. Set it to True or False inside the Sub based on success or failure. The calling code can then check its value to decide what to do next.

Do I need to declare all parameters as ByRef?

No. Only declare the ones you want to return data from as ByRef. Others can be ByVal if they’re only inputs. ByRef is the default in VBA, but explicitly declaring it improves clarity.

Can I return a worksheet object from a Sub?

Yes, declare the parameter as Range or Worksheet and use Set to assign it. For example: Set wsOutput = Worksheets("Sheet2"). The calling code can then use that object directly.