Trouble with Subroutine - Works fine the first time it is called.

rcbricker33

New Member
Joined
Oct 18, 2013
Messages
21
I have a function that I am calling a few times. I simply change one of the variables (the first worksheet). Beyond that the function should do exactly the same thing each time it is called. The function is being used to generate a list of data from the first worksheet by filtering using an array. It then copies and moves the data to sheet two, pastes it to the sheet and then compares the list to create a new list which represents data found on sheet one that should be in the next step. I run the function and it works great the first time (for the first sheet that needs to be filtered). When it comes time to do it for the next sheet that needs to be filtered I am getting an error. I get an error number 9 Subscript out of range. Not sure how to get this to work. I need to run this function on 4 separate worksheets.

Function is called from the main sub with the following:

VBA Code:
strFILTER = SUSfilter(wsUTBITD, wsMAP, arrSGL(), strFS, strLINE)

only the first worksheet variable is changed when the function is recalled.

VBA Code:
Function SUSfilter(ws As Worksheet, wsMAP As Worksheet, arrSGL() As String, strFS As String, _
            strLINE As String)
Dim lotbl As ListObject
Dim locSGL As ListColumn, locSUS As ListColumn
Dim lng As Long, lngROW As Long, lngCOL As Long
Dim rng As Range, rngHEAD As Range, cell As Range, rngSUS As Range
Dim intMSUS As Integer, intMSGL As Integer, intMLINE As Integer
Dim str As String, strFILTER As String
Dim varI As Variant

    ws.Activate
    With ws
        Set lotbl = ws.ListObjects(1)
        lotbl.AutoFilter.ShowAllData
        Set locSGL = lotbl.ListColumns("CBDP_SGL")
        lng = locSGL.Range.Column
        Set locSUS = lotbl.ListColumns("CBDP_SUS")
        ws.ListObjects("AFS_TB").Range.AutoFilter Field:=lng, Criteria1:=arrSGL, Operator:=xlFilterValues
        Set rng = locSUS.Range
        rng.Copy
        lotbl.AutoFilter.ShowAllData
    End With
    wsMAP.Activate
    With wsMAP
        wsMAP.AutoFilterMode = False
        lngROW = LASTrow(wsMAP)
        lngCOL = LASTCOL(wsMAP)
        wsMAP.Cells(1, lngCOL + 2).PasteSpecial xlValues
        Set rngHEAD = wsMAP.Range(wsMAP.Cells(1, 1), wsMAP.Cells(1, lngCOL))
        If strFS = "BS" Then
            intMLINE = rngHEAD.Find("CBDP line").Column
        Else
            intMLINE = rngHEAD.Find("Line").Column
        End If
        intMSGL = rngHEAD.Find("SGL").Column
        intMSUS = rngHEAD.Find("Id").Column
        lng = wsMAP.Cells(wsMAP.Rows.Count, lngCOL + 2).End(xlUp).Row
        Set rng = wsMAP.Range(wsMAP.Cells(1, lngCOL + 2), wsMAP.Cells(lng, lngCOL + 2))
        Application.CutCopyMode = False
        wsMAP.Range(rng.Address).RemoveDuplicates Columns:=1, Header:=xlYes
        wsMAP.Sort.SortFields.Clear
        wsMAP.Sort.SortFields.Add Key:=wsMAP.Cells(1, lngCOL + 2), SortOn _
                :=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        With wsMAP.Sort
            .SetRange rng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        lng = wsMAP.Cells(Rows.Count, lngCOL + 2).End(xlUp).Row
        Set rng = wsMAP.Range(wsMAP.Cells(1, 1), wsMAP.Cells(lngROW, lngCOL))
        wsMAP.Range(rng.Address).AutoFilter Field:=intMLINE, Criteria1:=strLINE
        Set rng = wsMAP.Range(wsMAP.Cells(1, lngCOL + 2), wsMAP.Cells(lng, lngCOL + 2))
        Set rngSUS = wsMAP.Range(wsMAP.Cells(1, intMSUS), wsMAP.Cells(lngROW, intMSUS))
        For Each cell In rng
            lng = Empty
            str = cell.Value
On Error Resume Next
            lng = rngSUS.Find(What:=str, After:=wsMAP.Cells(1, intMSUS), LookIn:=xlValues, LookAt _
                :=xlWhole).Row
On Error GoTo 0
            If lng = 0 Then
                cell.ClearContents
            Else
                strFILTER = strFILTER & cell.Value & ","
            End If
        Next cell
        wsMAP.AutoFilterMode = False
        rng.Delete
        If Right(strFILTER, 1) = "," Then
            strFILTER = Left(strFILTER, Len(strFILTER) - 1)
        End If
        SUSfilter = strFILTER
    End With
End Function

Thanks in advance for any help.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Which line causes the error?
 
Upvote 0
Which line causes the error?
my bad. I forgot to comment it.

following line causes the error. I used a for each and debugged the array. It is populated. It is also the same array used the first time it is called. This array doesnt change once it is populated.

VBA Code:
ws.ListObjects("AFS_TB").Range.AutoFilter Field:=lng, Criteria1:=arrSGL, Operator:=xlFilterValues
 
Upvote 0
The error typically means that the table name doesn't exist on that sheet.
 
Upvote 0
Solution
That is exactly the reason. ****. I have a hardcoded table name in the line.

Thanks for the extra set of eyes.
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,148
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top