I need your help.
I would like to Autofilter a column of a table with the following value: begins with AF. And then copy and paste some column to another sheet.
I have written a code but I alwasy get an error when the code reach the following line:
.AutoFilter Field:=rng0.Column, Criteria1:=SearchFor
The error is: Object variable or with block is not set.
I have no idea what is wrong with the code. Please help me.
Sub AF_update()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
SearchCol0 = "Prefix+short name"
SearchCol1 = "Site type"
SearchCol2 = "SLA Target"
SearchCol3 = "Mean Rtt (ms)"
SearchCol4 = "Max Rtt (ms)"
SearchCol5 = "Threshold 95%"
SearchCol6 = "Threshold 99%"
SearchFor = "=AF*"
Dim rng0, rng1, rng2, rng3, rng4, rng5, rng6 As Range
Dim lastrow As Long
Set rng0 = ActiveSheet.UsedRange.Find(SearchCol0, , xlValues, xlWhole)
Set rng1 = ActiveSheet.UsedRange.Find(SearchCol1, , xlValues, xlWhole)
Set rng2 = ActiveSheet.UsedRange.Find(SearchCol2, , xlValues, xlWhole)
Set rng3 = ActiveSheet.UsedRange.Find(SearchCol3, , xlValues, xlWhole)
Set rng4 = ActiveSheet.UsedRange.Find(SearchCol4, , xlValues, xlWhole)
Set rng5 = ActiveSheet.UsedRange.Find(SearchCol5, , xlValues, xlWhole)
Set rng6 = ActiveSheet.UsedRange.Find(SearchCol6, , xlValues, xlWhole)
Set Target = ThisWorkbook.Worksheets("AF")
Set Source = ThisWorkbook.Worksheets("RAW DATA")
Target.Select
Range("A2").Select
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Selection.ClearContents
Source.Select
If ActiveSheet.AutoFilterMode = True Then
Range("a1").AutoFilter
End If
Range("A1").Select
With Selection
.AutoFilter Field:=rng0.Column, Criteria1:=SearchFor
End With
rng0.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng1.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng2.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng3.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng4.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng5.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Select
rng6.Offset(1, 0).Select
Range(Selection, Selection.End(xlDown)).Copy
Target.Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
lastrow = Cells(Rows.Count, 5).End(xlUp).Row
Range("A2:G" & lastrow).Sort key1:=Range("E2:E" & lastrow), order1:=xlDescending, Header:=xlNo
Source.Select
ActiveSheet.AutoFilterMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
MsgBox "Operation Completed!"
End Sub