Error-Handling for Repeat VBA Code for Multiple Values

JohnHenry

New Member
Joined
Mar 12, 2013
Messages
27
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
 

rizvisa1

New Member
Joined
Feb 18, 2010
Messages
15
Instead of on error resume next, why not try to see if you got a match.
VehNumStart = 0
For VehNumRow = 3 To 3000
If Cells(VehNumRow, VehCol).Value = VehNum Then
VehNumStart = VehNumRow
Exit For
End If
Next VehNumRow
If (VehNumStart > 0) Then
'.. do rest
End If


How ever I think this would be more efficient way. I have not testing it though
''''''''''CODE START
Sub VehNum_Test()
Dim cell As Range

''''''''''Discover location of "Vehicle" column
Sheets("Data").Select

Set cell = Range("A1:Z100").Find(What:="Vehicle")
If cell Is Nothing Then
MsgBox "Not able to find vehicle column"
Exit Sub
End If

vehcol = cell.Column
VehRow = cell.Row

Dim VehNum As Variant, VehNumRow As Long, VehNumStart As Long, VehNumEnd As Long

Sheets("Data").AutoFilterMode = False
Range(Cells(2, vehcol), Cells(Rows.Count, vehcol)).AutoFilter

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)

Range(Cells(2, vehcol), Cells(Rows.Count, vehcol)).AutoFilter Criteria1:=VehNum
VehNumEnd = Cells(Rows.Count, vehcol).End(xlUp).Row
If (VehNumEnd > 2) Then
'''''''''''Paste data into correct month available cells
openrow = Sheets("January").Cells(Rows.Count, "A").Row
openrow = openrow + 1
Application.CutCopyMode = False
Range(Cells(3, "A"), Cells(VehNumEnd, "Z")).Copy
Sheets("January").Range("A" & openrow).PasteSpecial
Application.CutCopyMode = False
End If
Next

End Sub
'''''''''''CODE END
 

JohnHenry

New Member
Joined
Mar 12, 2013
Messages
27
Rizvisa1,

Thanks for your reply. It does look more efficient. However, I am receiving an error: "AutoFilter method of Range class failed" in the line "Range(Cells(2, vehcol), Cells(Rows.Count, vehcol)).AutoFilter Criteria1:=VehNum." Is there a way I can upload a copy of my sample data as excel file either to this thread or to you directly? Thanks again for helping.

John
 

rizvisa1

New Member
Joined
Feb 18, 2010
Messages
15
Here is tested one :)

Sub VehNum_Test()
Dim cell As Range

''''''''''Discover location of "Vehicle" column
Sheets("Data").Select

Set cell = Range("A1:Z100").Find(What:="Vehicle")
If cell Is Nothing Then
MsgBox "Not able to find vehicle column"
Exit Sub
End If

vehcol = cell.Column
VehRow = cell.Row

Dim VehNum As Variant, VehNumRow As Long, VehNumStart As Long, VehNumEnd As Long

Sheets("Data").AutoFilterMode = False
Range(Cells(2, vehcol), Cells(Rows.Count, vehcol)).AutoFilter

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)

Range(Cells(2, vehcol), Cells(Rows.Count, vehcol)).AutoFilter Field:=1, Criteria1:=VehNum
VehNumEnd = Cells(Rows.Count, vehcol).End(xlUp).Row
If (VehNumEnd > 2) Then
'''''''''''Paste data into correct month available cells
openrow = Sheets("January").Cells(Rows.Count, "A").End(xlUp).Row
openrow = openrow + 1
Application.CutCopyMode = False
Range(Cells(3, "A"), Cells(VehNumEnd, "Z")).Copy
Sheets("January").Range("A" & openrow).PasteSpecial
Application.CutCopyMode = False
End If
Next

End Sub
'''''''''''CODE END
 

JohnHenry

New Member
Joined
Mar 12, 2013
Messages
27
Rizvisa1,

It works beautifully, thank you very much. Maybe now I can meet my April 1st deadline for this report ;)

John
 

rizvisa1

New Member
Joined
Feb 18, 2010
Messages
15
You are quite welcome. Long time to April 1st. You still have about 6:20 odd minutes. Whats the rush :)
 

Forum statistics

Threads
1,081,995
Messages
5,362,610
Members
400,684
Latest member
Vie

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top