Repeating action for every cell in range

SVKBaki

New Member
Joined
Feb 9, 2011
Messages
6
Hi I would like to ask for help with following situation:

I have a code, which runs the operation (comparing the value of specified cell with values from other specified column and then write appropriate value to other cell.

This code is fine for one specified cell, however I have 5152 such cells which needs to be evaluated (N2:N5152).

What I need to do is to adjust the following code, so it will do the specified operation for first cell, then go down to other cell and do the same, and so for each of the 5152 cells in column.

The code is: (assigned to button)
Code:
Private Sub button_Click()
Dim i, Poz  As Integer
i = 2
Poz = 0
 
Do While Worksheets("sheet2").Range("A" & i) <> ""
   Poz = Poz + 1
   i = i + 1
 
Loop
For i = 2 To Poz
    If Worksheets("sheet1").Range("N2") = "0" Then
    Worksheets("sheet1").Range("O2") = "0"
 
    ElseIf Worksheets("sheet1").Range("N2") <= Worksheets("sheet2").Range("A" & i) Then
        Worksheets("sheet1").Range("O2") = Worksheets("sheet2").Range("G" & i)
        Exit For
        Else
    End If
 
Next i
End Sub

The code compares value in cell N2 with values in column A in sheet 2 and if it finds first which is higher or at least equal, then it writes the value of specified cell in column G.
I need to write it, so this will be repeated for every cell from N2 to N5152. I don´t know how to specify how to go to cell N3, N4... and repeat the process for them.

thanks for any help
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Perhaps:

Code:
For i = 2 To Poz
    Dim cell As Range
    For Each cell In Worksheets("sheet1").Range("N2:N5152")
        If cell.Value = "0" Then
            Worksheets("sheet1").Range("O2") = "0"
        ElseIf cell.Value <= Worksheets("sheet2").Range("A" & i) Then
            Worksheets("sheet1").Range("O2") = Worksheets("sheet2").Range("G" & i)
            Exit For
        End If
    Next
Next i
 
Upvote 0
thanks this works nicely :)

thread can be closed, situation solved, hope this will be helpful to anyone
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,629
Members
452,933
Latest member
patv

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