1

My loop doesn't seem to break. Not sure where am I going wrong. Cell C5 in tab "ABC" = 44.5%. Cell C4 in tab "ABC" = 32%. I have to list down percentages between 32% to 45% in column E. SO E9 = 32%, E10 = 33%, E22 = 45%. I have linked cell E9 to cell C4. My macro starts populating from cell E10. Cell C2 in tab "ABC" = 9

My macro keeps on running and doesn't stop at 45%.

Sheets("ABC").Select
i = Worksheets("ABC").Range("C2").Value + 1

Do Until Cells(i, 5).Value <= Round(Worksheets("ABC").Range("C5").Value, 0)
    Cells(i, 5).Value = Cells(i - 1, 5).Value + 0.01
    i = i + 1
Loop

End Sub
1
  • Shouldn't it be >= instead of <=? Commented Mar 14, 2017 at 15:26

3 Answers 3

1

You are steadily increasing Cells(i - 1, 5).Value by + 0.01.

So, Cells(i - 1, 5).Value gets bigger. Hence, I am assuming that the line:

Do Until Cells(i, 5).Value <= Round(Worksheets("ABC").Range("C5").Value, 0)

should really be:

Do Until Cells(i, 5).Value >= Round(Worksheets("ABC").Range("C5").Value, 0)

Furthermore, you are forgetting to set the starting point correctly. If I understand the post correctly then the starting point should be Range("C4").Value.

The rounding is not necessary as you are using >= and not =. So, it doesn't matter if there is an exact match. It merely needs to pass the threshold given in Range("C5").

Finally, Cells(i, 5).Value will be always ="" (empty) when the check is run because your loop will set Cells(i, 5).Value only after the check to a certain value. Hence, you need to verify the row before Do Until Cells(i - 1, 5).Value...

Bringing it all together I am guessing that you are looking for the following:

Option Explicit

Sub tmpSO()

Dim i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("ABC")
ws.Activate
i = ws.Range("C2").Value + 1

ws.Cells(i - 1, 5).Value2 = ws.Range("C4").Value2
Do Until ws.Cells(i - 1, 5).Value2 >= ws.Range("C5").Value2
    ws.Cells(i, 5).Value = ws.Cells(i - 1, 5).Value + 0.01
    i = i + 1
Loop

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Doing this skips the loop and ends the macro.
0

Try this:

Sheets("ABC").Select
i = Worksheets("ABC").Range("C2").Value + 1

Do Until Cells(i - 1, 5).Value >= Worksheets("ABC").Range("C5").Value
    Cells(i, 5).Value = Cells(i - 1, 5).Value + 0.01
    i = i + 1
Loop

Comments

0

Ralph & Pspl,

Many thanks!! Your suggestions helped and worked for me. This is an excellent forum for new coders like me. Thanks again for your help.

For the benefit of the larger group, this is what I have finally used. 

Sheets("ABC").Select
i = Worksheets("ABC").Range("C2").Value + 1

Do Until Cells(i - 1, 5).Value >= Worksheets("ABC").Range("C5").Value
    Cells(i, 5).Value = Cells(i - 1, 5).Value + 0.01
    i = i + 1
Loop

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.