Help with loop

th081

Board Regular
Joined
Mar 26, 2006
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a loop like below:

For i = 2 to Ubound(Unit)
m = 0

Do
m = m + 1
Loop until Data(m,1) = Unit(i,1) and Data(m,4) = NoC(i,1)

'then do something once you found match


Next i

However sometimes there is no match and i want it to log on a sheet named error for which Unit(i,1) there was no match and then continue. How would i capture that in the loop.

Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Here is one approach
Code:
For i = 2 to Ubound(Unit)
    m = 0
    Do 
        m = m + 1
    Loop until Data(m,1) = Unit(i,1) and Data(m,4) = NoC(i,1)
    [COLOR=#ff8c00]If Data(m, 1) <> Unit(i, 1) Or Data(m, 4) <> NoC(i, 1) Then
        sheets("error").Cells(Rows.Count, 1).End(xlUp)(2) = Unit(i, 1) & "Not matched"
    End If[/COLOR]
 'then do something once you found match
 Next i

You can play with the way it posts to the error sheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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