Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA returning error when calling a Sub with multiple parameters

Tags:

excel

vba

I'm trying to figure out why VBA is returning an error (Compile error: Expected: =)when I call a Sub and supply it with multiple parameters.

Sub customerController(cleanStructure As Boolean, firstCol As Integer, latCol As Integer, _
                    lngCol As Integer, Optional startRow As Long, Optional endRow As Long)

Dim i As Long, j As Long, n As Long

If (cleanStructure = False) Then
    'customer data type
    If (startRow = "") Then i = 1
    If (endRow = "") Then j = countRows
    For n = i To j - i + 1
        generateURL(n, firstCol)
        newReadXMLData (url)
        ActiveSheet.Cells(i, latCol).Value = lat
        ActiveSheet.Cells(i, lngCol).Value = lng
    Next
End If

End Sub

The Sub that I'm calling requires two parameters:

Sub generateURL(row As Long, column As Long)
like image 653
MarcinAu Avatar asked Nov 29 '12 11:11

MarcinAu


People also ask

How do you pass parameters in VBA sub?

VBA allows you to pass variables into subroutines and functions in two ways. You can specify either ByVal or ByRef for each of the variables that are passed in. The ByVal and ByRef distinction for subroutine and function parameters is very important to make. In VBA all objects are passed by reference.

Can VBA subs take arguments?

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.

How do you call a sub function in VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses.

What does () with sub function do in VBA?

A sub can be described as a small program within the VBA Editor that performs a specific action in Excel. It is used to break large pieces of code into smaller parts that can be easily managed.


1 Answers

When calling more than 1 parameter (i.e. just generateURL(n) works) you need to either use

  • Call generateURL(n, firstCol) , or
  • generateURL n, firstCol

using Call is the better programming technique as it is clearer

As per MSDN:

You normally use the Call statement to call a procedure that does not return a value. If the procedure returns a value, the Call statement discards it. You are not required to use the Call statement when calling a procedure. However, it improves the readability of your code.

like image 140
brettdj Avatar answered Oct 12 '22 18:10

brettdj