Search code examples
excelvbado-loops

Do Until specified value is reached in another cell


I'd like to concatenate the number of days that add up to 80%. Please see the example below;

enter image description here

I can run a code that concatenates Range A1:A7 and the result is printed in C1;

Sub Concatenator()

Dim lastLng As Long
Dim result As String
Dim delim As String
Dim b As String

delim = "&"

lastLng = Worksheets("Sheet1").Range("A1048576").End(xlUp).Row

For i = 1 To lastLng

b = Cells(i, 1).Value
result = result & b & delim

Next

result = Left(result, Len(result) - Len(delim))

Worksheets("Sheet1").Cells(1, 3).Value = result

End Sub

I'd to add a "Do Until" loop that loops until the value in column is greater than 80%. I've tried to amend the code above with the "Do Until" loop;

Sub Concatenator()

Dim lastLng As Long
Dim result As String
Dim delim As String
Dim b As String

delim = "&"

lastLng = Worksheets("Sheet1").Range("A1048576").End(xlUp).Row

Do Until Cells(i, 2).Value = ">80%"

For i = 1 To lastLng1

b = Cells(i, 1).Value
result = result & b & delim

Next

Loop

result = Left(result, Len(result) - Len(delim))

Worksheets("Sheet1").Cells(1, 3).Value = result

End Sub

Solution

  • As far as I understand this might work for you To understand how to set up the code, take a look For-Loop and Do-while, then combine the conditions with the loop as the following code

    i = 1
    Do Until Cells(i, 2).Value = 0.8 'Loop until request condition
        If i > lastLng1 Then Exit Do 'Loop until end of the range
        b = Cells(i, 1).Value
        result = result & b & delim
        i = i + 1
    Loop
    
    'Or------------------------------
    
    For i = 1 To lastLng1                        'Loop until end of the range
        If Cells(i, 2).Value = 0.8 Then Exit For 'Loop until request condition
        b = Cells(i, 1).Value
        result = result & b & delim
    Next