How to execute each statement in a For..Next Loop using an Error Handler to provide default values where there is error?

TheBlueMusketeer

New Member
Joined
Sep 29, 2012
Messages
12
Excel Gurus,
I need your help with this one. I have a For..Next loop using which I want to search for strings stored in an array within a particular sheet. Basically I am looking for the row location of each string in the array.

For example:

Sub DataCollect()
On Error Goto ErrHandler
'....Assume I used a loop to input 100 different strings into an array called "Array"
For i = 1 to 100

Row1 = Cells.Find(Array(i), SearchOrder:=xlbyRows, SearchDirection:=xlNext).Row

Sum1 = Sum1 + Cells(Row1, 3)

Next i

'This is what I need: If Array(i) is not in the sheet, I want to use the error handler to set the value of Sum1 as:
'Sum1 = Sum1 + 0
'and continue with the loop. Please advise as to how I can accomplish that. Doing the following, exits the sub. I want the loop to work as normal in case the searched value is not to be found.

Exit Sub
ErrHandler:
Sum1 = Sum1 + 0

End Sub
End

Thanks for your help,
S
Excel 2007

PS: I used On Error Resume Next but it screws up my program as I have some intermediate code in between.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
One way to do this is
Code:
    Row1 = 0
    On Error Resume Next
    Row1 = Cells.Find(Array(i), SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
    On Error GoTo 0
    If Row1 <> 0 Then Sum1 = Sum1 + Cells(Row1, 3)

Another way, with an implicit error trap:
Code:
    Dim FoundCell As Range
    Set FoundCell = Cells.Find(Array(i), SearchOrder:=xlByRows, SearchDirection:=xlNext)
    If Not FoundCell Is Nothing Then _
        Sum1 = Sum1 + Cells(FoundCell.Row, 3)
And, dispensing with the intermediate variable altogether:
Code:
    On Error Resume Next
    Sum1 = Sum1 + Cells(Cells.Find(Array(i), SearchOrder:=xlByRows, SearchDirection:=xlNext).Row, 3)
    On Error GoTo 0
Excel Gurus,
I need your help with this one. I have a For..Next loop using which I want to search for strings stored in an array within a particular sheet. Basically I am looking for the row location of each string in the array.

For example:

Sub DataCollect()
On Error Goto ErrHandler
'....Assume I used a loop to input 100 different strings into an array called "Array"
For i = 1 to 100

Row1 = Cells.Find(Array(i), SearchOrder:=xlbyRows, SearchDirection:=xlNext).Row

Sum1 = Sum1 + Cells(Row1, 3)

Next i

'This is what I need: If Array(i) is not in the sheet, I want to use the error handler to set the value of Sum1 as:
'Sum1 = Sum1 + 0
'and continue with the loop. Please advise as to how I can accomplish that. Doing the following, exits the sub. I want the loop to work as normal in case the searched value is not to be found.

Exit Sub
ErrHandler:
Sum1 = Sum1 + 0

End Sub
End

Thanks for your help,
S
Excel 2007

PS: I used On Error Resume Next but it screws up my program as I have some intermediate code in between.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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