On Error loop to next 'x'

blacktour

Board Regular
Joined
May 24, 2005
Messages
219
I have a program running and I am having problems with error handling. The program is in a loop, it loops through numbers on a spreadsheet looks up the numbers in a website if the number is there it works flawlessly. If the number isn't there it gives an error.

It is a For x = 5 to 'Max' (variable)

When any kind of error hits I want it to look to the next x so if it is on 15 I want it to skip it and go to 16? Any ideas how to accomplish this?

Thanks,
Mike
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

blacktour

Board Regular
Joined
May 24, 2005
Messages
219
On Error Resume Next ignores the error and processes the next line of code. If I use a On Error Go To ErrorHandle

ErrorHandle:
'leave blank


Next X

This still doesn't work. It just tries the same row again instead of going to the next line in the spreadsheet?
 
Upvote 0

blacktour

Board Regular
Joined
May 24, 2005
Messages
219
Effectively what I want is the following:

If Error Occurs Then
Next X
End If

I can't figure out how to use the Error Handling to make this happen?
 
Upvote 0

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,155
okay. With this data:
Excel Workbook
A
12
23
34
4A
52
63
Sheet1
Excel 2003

Rich (BB code):
Sub exa()
Dim i As Long, a As Long
    
    On Error Resume Next
    For i = 1 To 7
        a = a + Cells(i, 1)
    Next
End Sub

...would seem to do what you want?
 
Last edited:
Upvote 0

gaj104

Well-known Member
Joined
Nov 9, 2002
Messages
864
Maybe take a step back. When you say error occurs. What exactly is occurring.

If its a recurring actual outcome (i.e. it is not there) then you should be able to handle it.

Error handling is usually for unexpected errors.

If there wasn't the existance of "Not there" in the A column this code would error:

Code:
For i = 1 To 10

     Set r = Range("A:A").Find("Not there")


Next i

To handle this, this code handles where the value "Not there" in the A column. If it doesn't exist then it goes to the next i item

Code:
For i = 1 To 10

    If Not Range("A:A").Find("Not there") Is Nothing Then
    
        'do code here
    
    End If

Next i
 
Last edited:
Upvote 0

blacktour

Board Regular
Joined
May 24, 2005
Messages
219
I think that is what I want so if there was an error that occurred in the a = a + Cells(i, 1)
then I want it to loop to the next 'i' in this case. Mine is not looping to the next 'i' it is just skipping the error and processing the next line of code?
 
Upvote 0

blacktour

Board Regular
Joined
May 24, 2005
Messages
219
Maybe take a step back. When you say error occurs. What exactly is occurring.

If its a recurring actual outcome (i.e. it is not there) then you should be able to handle it.

Error handling is usually for unexpected errors.

If there wasn't the existance of "Not there" in the A column this code would error:

Code:
For i = 1 To 10

     Set r = Range("A:A").Find("Not there")


Next i

To handle this, this code handles where the value "Not there" in the A column. If it doesn't exist then it goes to the next i item

Code:
For i = 1 To 10

    If Not Range("A:A").Find("Not there") Is Nothing Then
    
        'do code here
    
    End If

Next i



The error actually occurrs when it logs onto the website and tries to look up the value from A5 on the spreadsheet. Then it crashes. Is there not way to say if there is any error just skip all the code and skip to A6?
 
Upvote 0

Forum statistics

Threads
1,191,515
Messages
5,987,004
Members
440,074
Latest member
Emmanuelian

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
Top