kmmsquared
New Member
- Joined
- Jan 7, 2011
- Messages
- 33
Hi,
I am attempting to run the following code: If I don't have the variables present in the AutoFilter, Excel gives me an error and doesn't go to the next step--it only works the first time. Any ideas on why this is and how to fix it?
Any help would be greatly appreciated.
Thanks!
I am attempting to run the following code: If I don't have the variables present in the AutoFilter, Excel gives me an error and doesn't go to the next step--it only works the first time. Any ideas on why this is and how to fix it?
Code:
Sub Latest_Scheduled_Delivery_Date()
'Copy/Paste Cell Values
Columns("T:V").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Dim lngLastRow As Long
With Sheets("BOM")
lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Insert Row
Columns("W:W").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Insert Formula into new Row
Range("W2:W" & lngLastRow).Formula = "=IF(AND(T2<>"""",U2<>"""",V2<>"""",EXACT(T2,U2),EXACT(U2,V2)),1,IF(AND(T2<>"""",U2<>"""",V2<>"""",T2<>U2,U2=V2),2,IF(AND(T2<>"""",U2<>"""",V2<>"""",T2<>U2,U2<>V2),3,IF(AND(T2<>"""",U2<>"""",V2<>"""",EXACT(T2,U2),U2<>V2),4,IF(AND(T2<>"""",U2<>"""",V2="""",T2<>U2),5,IF(AND(T2<>"""",U2<>"""",V2="""",EXACT(T2,U2)),6,IF(AND(T2="""",U2="""",V2<>""""),7,IF(AND(T2="""",U2="""",V2=""""),8,""""))))))))"
Columns("W").Select
Selection.NumberFormat = "General"
Columns("W").Copy
Columns("W").Select
Selection.PasteSpecial Paste:=xlPasteValues
'Autofilter
Columns("W").Select
Selection.AutoFilter Field:=1, Criteria1:="1", Operator:=xlOr, Criteria2:="2"
[COLOR=red]On Error GoTo Step2:[/COLOR]
Range("V2:V" & lngLastRow).SpecialCells(xlCellTypeVisible).Select
Selection.ClearContents
Step2:
Worksheets("BOM").AutoFilterMode = False
Columns("W").Select
Selection.AutoFilter Field:=1, Criteria1:="3", Operator:=xlOr, Criteria2:="4"
[COLOR=red]On Error GoTo Step3:[/COLOR]
Range("V2:V" & lngLastRow).SpecialCells(xlCellTypeVisible).Select
Selection.Interior.ColorIndex = 22
Step3:
Worksheets("BOM").AutoFilterMode = False
Columns("W").Select
Selection.AutoFilter Field:=1, Criteria1:="4", Operator:=xlOr, Criteria2:="6"
[COLOR=red]On Error GoTo Step4:[/COLOR]
Range("T2:T" & lngLastRow).SpecialCells(xlCellTypeVisible).Select
Selection.ClearContents
Step4:
Worksheets("BOM").AutoFilterMode = False
Columns("W").Select
Selection.AutoFilter Field:=1, Criteria1:="7"
[COLOR=red]On Error GoTo Step5:[/COLOR]
Range("U2:U" & lngLastRow).SpecialCells(xlCellTypeVisible).Select
Selection.Formula = "=RC[1]"
Worksheets("BOM").AutoFilterMode = False
Columns("U").Copy
Columns("U").Select
Selection.PasteSpecial Paste:=xlPasteValues
Columns("W").Select
Selection.AutoFilter Field:=1, Criteria1:="7"
Range("V2:V" & lngLastRow).SpecialCells(xlCellTypeVisible).ClearContents
Worksheets("BOM").AutoFilterMode = False
Columns("W").Delete
Step5:
End With
End Sub
Any help would be greatly appreciated.
Thanks!