执行直到在另一个单元格中达到指定值

Do Until specified value is reached in another cell

我想连接总计 80% 的天数。请看下面的例子;

我可以 运行 连接范围 A1:A7 的代码,结果打印在 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

我想添加一个 "Do Until" 循环,循环直到列中的值大于 80%。我试图用 "Do Until" 循环修改上面的代码;

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

据我所知这可能对你有用 要了解如何设置代码,请看一下For-Loop and Do-while,然后将条件与循环结合起来,如下代码

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

这对我有用

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
    If Cells(i, 2).Value > "80" Then Exit For
    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

我将列中的值从百分比更改为数字。我也去掉了等号

For i = 1 To lastLng
    If Cells(i, 2).Value > "80" Then Exit For
    b = Cells(i, 1).Value
    result = result & b & delim
Next

目前这对我来说效果很好。谢谢@The GridLock