Help with For Each ..... Next Loop

abdul hafeel

New Member
Joined
Oct 14, 2006
Messages
9
I have a named ranges
eg
Range1 ="A1:C10" & Range2 = "A1:B1"

I need to find if the value in Range2 occurs in Range1.
I Ran the following code but it doesn't work. Pls help

Dim a, b, k As Integer
k = 0
a = 0
Do Until a = Range2.Columns.Count
b = 0
Do Until b = Range1.Rows.Count
For Each Range2(a+1, b+1) In Range1(a + 1, b + 1)
If p = pRange1(a + 1, b + 1) Then
k = k + 1
End If
Next Range2(a+1, b+1)
b = b + 1
Loop
a = a + 1
Loop

MsgBox k
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
What are you trying to do here? This is not coded correctly, whatever it is:
Code:
For Each Range2(a+1, b+1) In Range1(a + 1, b + 1)

Can't you just use COUNTIF?
 
Upvote 0

Forum statistics

Threads
1,215,583
Messages
6,125,665
Members
449,247
Latest member
wingedshoes

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