Search code examples
arraysexcelvbastatements

compiler error: Expected: end of statement when using "To" statement for array range excel vba


For a project I am working on I'm using a param array to grab string values from a user and then run an autofilter incorporating string values from the array. However, whenever I use a To statement(E.G. Boundaries = LBound(ArrayHere) To UBound(ArrayHere)) I get the following error:

Screenshot: Compiler Error when using "To" statement in variable.

Code:

Sub FilterWorkbookByServiceCode(FileName As String, WorkSheetName As String, Start As String, EndColumn As String, Field As Integer, ParamArray Numbers() As Variant)

    Dim Boundaries As Long

    Boundaries = LBound(Numbers) To Ubound(Numbers) 'Line Compiler error occurs on.

    Workbooks(FileName).Worksheets(WorkSheetName).Activate
    Range(Start).End(xlDown).Select
    Range(EndColumn & ActiveCell.Row).Select


    With Range(Start & ":" & ActiveCell.Address)
        .Select
        .AutoFilter Field:=Field, Criteria1:=Numbers(), Operator:=xlFilterValues
    End With
End Sub

I'm stumped on what I should do to correct this issue so any help is appreciated.

Edit:

To be a bit more clear, I'm trying to get the range of the param array, and then store it in a variable. I need to find the range of the array dyanmically as the user can input varying amount of values into the array, and then call each value from the array to use as the criteria for the autofilter.


Solution

  • I don't see boundaries used after that statement. So, remove the statement that is causing the error.

    Pass numbers to your function in quotes. i.e.

    FilterMySheetWithTheseValues("1","2",3") where it is defined as

    Sub FilterMySheetWithTheseValues(paramarray numbers() as variant)
       Range("A1:A10").AutoFilter 1, numbers, xlFilterValues
    End Sub
    

    Edit:

    Change the call from .AutoFilter Field:=Field, Criteria1:=Numbers(), Operator:=xlFilterValues

    to .AutoFilter Field, Numbers, xlFilterValues