I have this routine which has worked fine until I added avriable for the worksheet to use.
[vba]Sub DeleteRows(ws As Worksheet, criteria As String, col As Long)
Dim rTable As Range
Application.ScreenUpdating = False
On Error Resume Next
'Determine the table range
With Selection
If ws.Cells.Count > 1 Then
Set rTable = Selection
Else
Set rTable = .CurrentRegion
On Error GoTo 0
End If
End With
'Remove any existing AutoFilters
ws.AutoFilterMode = False
'Filter table based on vCriteria using the relative column position stored in lCol.
rTable.AutoFilter Field:=col, Criteria1:=criteria
'Delete all rows that are NOT hidden by AutoFilter.
rTable.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'Remove AutoFilters
ws.AutoFilterMode = False
On Error GoTo 0
End Sub[/vba]
I have tried calling the routine linke this
(Where 'parent' is correctly defined)
[vba]DeleteRows(Worksheets("Treeview"), parent, 2)[/vba]
And I have tried calling it like this
[vba]DeleteRows(wsTree,parent,2)[/vba]
Where the worksheet is defined as
[vba]Public wsTree as worksheet
Set wsTree = thisworkbook.worksheets("TreeView")[/vba]