I think the code is fairly self-explanatory, but I'm trying to search for a number (VehNum) in a column (VehCol) and copy all of the rows with the same number in the that column to a new sheet. I'm receiving a "Run-time error 1004: Application-defined or object-defined error" in 14th line of code: "If Cells(VehNumRow, VehCol).Value = VehNum Then." If the numbers in my array exist in the sheet, it works perfectly. However, the numbers in the code can't change day-to-day but their existence on the sheet do. I'm aware of different error-handling procedures like: "On Error Resume Next" but am unsure where to implement the line or of a more feasible method. Any help would be appreciated.
'''''''''''CODE START
Sub VehNum_Test()
''''''''''Discover location of "Vehicle" column
Sheets("Data").Select
Range("A1:Z100").Select
Cells.Find(What:="Vehicle").Select
VehCol = Selection.Column
VehRow = Selection.Row
Sheets("Data").Select
Dim VehNum As Variant, VehNumRow As Long, VehNumStart As Long, VehNumEnd As Long
For Each VehNum In Array(5, 8, 9, 20, 24, 33, '39, 43, 55, 75, _
76, 77, 83, 84, 85, 86, 509, 510, 511, 752)
Sheets("Data").Select
For VehNumRow = 3 To 3000
If Cells(VehNumRow, VehCol).Value = VehNum Then
VehNumStart = VehNumRow
Exit For
End If
Next VehNumRow
'''''''''''Figure out where the "Vehicle VehNum" data ends.
For VehNumRow = VehNumStart To 3000
If Cells(VehNumRow, VehCol).Value <> VehNum Then
VehNumEnd = VehNumRow
Exit For
End If
Next VehNumRow
VehNumEnd = VehNumEnd - 1
'''''''''''Paste data into correct month available cells
Sheets("January").Select
OpenRow = 2
Do While Cells(OpenRow, "A") <> ""
OpenRow = OpenRow + 1
Loop
Sheets("Data").Select
Cells(VehNumStart, "A").Select
Range(Selection, Cells(VehNumEnd, "Z")).Copy
Sheets("January").Select
Range("A" & OpenRow).PasteSpecial
Application.CutCopyMode = False
Next
End Sub
'''''''''''CODE END
'''''''''''CODE START
Sub VehNum_Test()
''''''''''Discover location of "Vehicle" column
Sheets("Data").Select
Range("A1:Z100").Select
Cells.Find(What:="Vehicle").Select
VehCol = Selection.Column
VehRow = Selection.Row
Sheets("Data").Select
Dim VehNum As Variant, VehNumRow As Long, VehNumStart As Long, VehNumEnd As Long
For Each VehNum In Array(5, 8, 9, 20, 24, 33, '39, 43, 55, 75, _
76, 77, 83, 84, 85, 86, 509, 510, 511, 752)
Sheets("Data").Select
For VehNumRow = 3 To 3000
If Cells(VehNumRow, VehCol).Value = VehNum Then
VehNumStart = VehNumRow
Exit For
End If
Next VehNumRow
'''''''''''Figure out where the "Vehicle VehNum" data ends.
For VehNumRow = VehNumStart To 3000
If Cells(VehNumRow, VehCol).Value <> VehNum Then
VehNumEnd = VehNumRow
Exit For
End If
Next VehNumRow
VehNumEnd = VehNumEnd - 1
'''''''''''Paste data into correct month available cells
Sheets("January").Select
OpenRow = 2
Do While Cells(OpenRow, "A") <> ""
OpenRow = OpenRow + 1
Loop
Sheets("Data").Select
Cells(VehNumStart, "A").Select
Range(Selection, Cells(VehNumEnd, "Z")).Copy
Sheets("January").Select
Range("A" & OpenRow).PasteSpecial
Application.CutCopyMode = False
Next
End Sub
'''''''''''CODE END