ON Error GoTo Label 'In a loop'

CodyMonster

Board Regular
Joined
Sep 28, 2009
Messages
159
Maybe someone can help me with this?
I have a loop going through a column to see if anything is not zero.
If there is an error in one of the cells I want is to go to the error handler and perform a set of statements.
However, I'm getting issues with the loop not ending as the variable will not end.

For example, if Lastrow = 10, when the next x runs it will continue to go to 11 or forever.
I don't know if I can use the GoTo label as I did. Using a If Than Else statement.

This isn't probably the prettiest script.. but it works (well, used too before I added the error handling).

Code:
Dim Lastrow As Integer

Lastrow = Sheets("Prior Data").Cells(Rows.Count, "B").End(xlUp).Row
    Debug.Print Lastrow
    
    For x = 1 To Lastrow
    Debug.Print x
            On Error GoTo ErrorLoop
            Creditlimitamount = Sheets("Prior Data").Cells(x, 5).Value
            If Creditlimitamount <> 0 Then
            Sheets("Prior Data").Cells(x, 2).Resize(1, 8).Copy
            Lastrowdiff = Sheets("differences").Cells(Rows.Count, 2).End(xlUp).Row + 1
            Sheets("differences").Range("B" & Lastrowdiff).PasteSpecial xlValues
            Sheets("differences").Range("B" & Lastrowdiff).PasteSpecial xlFormats
            Sheets("differences").Range("A" & Lastrowdiff).Value = Date
           End If

ErrorLoop:
If Application.WorksheetFunction.IsError(Sheets("Prior Data").Cells(x, 4)) Then
Sheets("Prior Data").Cells(x, 1).Resize(1, 3).Copy
Newcp = Sheets("Historical_Data").Cells(Rows.Count, 30).End(xlUp).Row + 1
Sheets("Historical_Data").Range("AD" & Newcp).PasteSpecial xlValues
Sheets("Historical_Data").Range("AD" & Newcp).PasteSpecial xlFormats

Else
Sheets("Prior Data").Cells(x, 1).Resize(1, 2).Copy
RemoveCp = Sheets("Historical_Data").Cells(Rows.Count, 34).End(xlUp).Row + 1
Sheets("Historical_Data").Range("AH" & RemoveCp).PasteSpecial xlValues
Sheets("Historical_Data").Range("AH" & RemoveCp).PasteSpecial xlFormats
Sheets("Prior Data").Cells(x, 4).Resize(1, 1).Copy
RemoveCp2 = Sheets("Historical_Data").Cells(Rows.Count, 36).End(xlUp).Row + 1
Sheets("Historical_Data").Range("AJ" & RemoveCp2).PasteSpecial xlValues
Sheets("Historical_Data").Range("AJ" & RemoveCp2).PasteSpecial xlFormats


End If

Skip:
Next x

Resume Skip


Thanks for all the help if you can!!!
 
Thanks mikerickson

I'm out of pocket right now but will be able to test this out on Weds.
I'll report back and let you know how it goes.
Thanks!!!


Code:
Creditlimitamount = Val(CStr(Sheets("Prior Data").Cells(x, 5).Value))
Will protect against an error value in Cells(x,5) and insure that Credtitlimitamount is numeric.
The CSTR will change an error value (like #DIV/0) into a string (e.g. "Error 2007") and the Val will turn that into 0.
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,532
Messages
6,172,878
Members
452,486
Latest member
standw01

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