PDA

View Full Version : Loop on Single cell



gally
06-02-2022, 10:56 AM
Hi

I’m relatively new to VBA and I’m currently stuck on creating a loop function. I have an IF statement (below) written. Where I need to increase a number (which is an option number) in the active cell until the criteria of another cell (4 to the right of active cell). I’ve had to use double rather than integer due to my value of the dependant cell (score) have decimals involved.


Sub Name()
Dim score As Double
score = ActiveCell.Offset(0,4).value
If score >=2 Then
ActiveCell = ActiveCell +1
End If
End Sub

I have tried Do/Loop While score >= 2 and when the dependant cell starts on <2 it doesn’t add anything (which is correct) however when starting on a value >=2 it keeps the loop going even when a number <2 has come in the dependant cell

Hope this all makes sense and appreciate any help

georgiboy
06-06-2022, 12:55 AM
Hi gally,

Welcome to the forum.

If your data was in column A then maybe something like the below:

Sub test()
Dim rng As Range, rCell As Range

Set rng = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

For Each rCell In rng.Cells
If rCell.Offset(, 4).Value >= 2 Then rCell.Value = rCell.Value + 1
Next rCell
End Sub

Hope this helps