PDA

View Full Version : Error Handling When Range Is Not Defined Properly



bobkap
05-13-2013, 08:09 AM
I have a macro with a vlookup statement and I know that from time to time the macro is going to bomb because I know that my users will not always correctly update the range the lookup statement refers to. When the program bombs, I want there to just be either a blank cell or a zero and ANYTHING other than a number populated in the targeted cell where my result goes. I've tried using "On Error Resume Next" in my code. This prevents my macro from crashing, but for some reason, the last legitimate answer from the previous row is entered in the row where it should bomb. So for example, the cell of the previous row shows $50 which is correct, but then in the next row where it should show nothing or a zero, it too shows $50.

The relevant code is as follows:


For counter = custrow To finalrowbilling
custname = Worksheets("billing").Cells(custrow, custcol)
classname = Worksheets("billing").Cells(custrow, classcol)
On Error Resume Next
Price = Application.WorksheetFunction.VLookup(custname, bk, WorksheetFunction.Match(classname, bk2, 0), False)
Worksheets("Billing").Cells(custrow, finalcolbilling).Offset(0, 1) = Price
custrow = custrow + 1
Next counter


Any help would be greatly appreciated.

Excel Fox
05-13-2013, 04:48 PM
Try
For counter = custrow To finalrowbilling
custname = Worksheets("billing").Cells(custrow, custcol)
classname = Worksheets("billing").Cells(custrow, classcol)
On Error Resume Next
Price = Application.WorksheetFunction.VLookup(custname, bk, WorksheetFunction.Match(classname, bk2, 0), False)
If Err.Number=0 Then
Worksheets("Billing").Cells(custrow, finalcolbilling).Offset(0, 1) = Price
End If
Price = 0
Err.Clear:On Error GoTo 0
custrow = custrow + 1
Next counter

bobkap
05-14-2013, 01:02 AM
I hope I am not violating one of your site rules by sending this message, but thanks VERY much. It works beautifully now!!!