Error Handling in VBA

cbarless

New Member
Joined
Mar 11, 2004
Messages
34
Hello

Below code is using the find method to search through a list and when an error occurs (find does not locate) it will copy over the missing information from one workbook to another. My issue is the first time the function does not find the value in the list it is not going to the error handling section (result is empty).


Set ws_shipments = Workbooks("RailShipments2013_14.xlsm").Worksheets("Database")
Set ws = Workbooks("AberdeenShipments13.xlsm").Worksheets("Tables")
Set station_range = ws.Columns("Y")
Set result = ws.Range("Y1")
lastspec = ws_shipments.Cells(ws_shipments.Rows.Count, "B").End(xlUp).Row

For row_count = 36000 To lastspec
lookfor = ws_shipments.Cells(row_count, "B").Value

On Error GoTo add_spec
Set result = station_range.Find(lookfor, result, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

Next row_count

Exit Sub

add_spec:
lastcell = ws.Cells(ws.Rows.Count, "Y").End(xlUp).Row
addnew = lastcell + 1
'copy information from ws_shipments to ws


The data is very long so I have it starting at line 36000, the first value which should reqult in the error is at line 36951. Code is working as expected for all rows which should be copied over following line 36951.

What am I missing?
 

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top