For loop in a for loop

cicconmr

Board Regular
Joined
Jul 5, 2011
Messages
90
From my knowledge of other programming languages this should seem simple enough....but of course I can't make it work...

My goal is to take one array of data, store the value then see if that value is in another array by looping through that array.

If it finds it, it simply adds 1 to a specific column. I define all the variables except cell and cellf because I'm sure not 100% sure how these for loops work?

Code:
    For Each Cell In Worksheets(1).Range("E2:E40")
        mcTicker = Cell.Value
        For Each CellF In Worksheets(3).Range("B3:B503")
            mcTicker2 = CellF.Value
            If mcTicker = mcTicker2 Then
                Range("M" & Cell.Row) = Range("M" & Cell.Row).Value + 1
            End If
        Next
    Next

Any ideas?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I would simplify your FOR loops so it's easy to step through it with OFFSET


For x = 1 to 8
mcTicker = Cell.Value
For y = 1 to 503
mcTicker2 = CellF.Value
If mcTicker = mcTicker2 Then
ActiveCell.Offset(0, 12).Select
mcTicker3 = Cellx.Value
Cellx = Cellx + 1
ActiveCell.Value = Cellx
ActiveCell.Offset(0, -12).Select
Else
End If
Next
Next
 
Upvote 0
Hi

cicconmr - You're doing fine. You define Cell and CellF as follows -

Code:
Dim Cell as Range
Dim CellF as Range

The loops work by progressing through each cell in each specified range.
For Cell it goes E2, E3, E4 ....E40 and
For CellF it goes B3, B4, B5 ...B503.

I would say for good practice that you need to qualify the Next statements with CellF and Cell in that order.

cjcobra - There is a flaw in your solution.
What Sheet and what Column is the Active Cell on?
You don't need to Select - the OP hasn't!
Where do Cellx and mcTicker3 come from?

hth
 
Upvote 0
Hi

Further to my previous post you need to change as follows -

Also you need to qualify the range where you add to the cell value eg -
Code:
 Worksheets(1).Range("M" & Cell.Row) = Worksheets(1).Range("M" & Cell.Row).Value + 1

change to "Worksheets(3)." if I have assumed incorrectly.

hth
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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