我希望代码的这一部分要做的是,如果单元格H83的值大于22,那么我希望代码将单元格R14的值0.7增加0.01,直到R14达到0.75或。直到H83小于22。我尝试过:
For j = 0.69 To 0.74
w = j + 0.01
If Range("h83") > 22 Then
Range("r14").Value = w
If 21 < Range("h83") < 23 Then Exit For
End If
Next j
这不起作用,现在我有了它,因此它仅将它一次增加0.01(星号),完整代码:
Sub C_CreateTestResultTableV2()
Application.ScreenUpdating = True 'helps the code run faster
Dim vInputs, vResults()
Dim c As Integer, i As Integer
'create INPUTS
c = Range("b5").End(xlToRight).Column
vInputs = Range("b5", Cells(9, c))
'determine last value in the column
c = UBound(vInputs, 2)
'create RESULTS
ReDim vResults(1 To 4, 1 To c)
For i = 1 To c
'checks to see if t_air_in > 22
If vInputs(1, i) > 22 And vInputs(3, i) < 70 Then
'set values
Range("j18") = vInputs(1, i)
Range("n14") = vInputs(3, i)
Range("r16") = vInputs(5, i)
'checks to see if t_air_out = 22 and changes t_wat_in and m_wat_in accordingly
If Range("h83") > 22 Then
Range("r16").Value = Range("r16").Value - 3
End If
*If Range("h83") > 22 Then
Range("r14").Value = Range("r14").Value + 0.01
End If*
'copy output values into RESULTS
vResults(1, i) = Range("h83")
vResults(2, i) = Range("k83")
vResults(3, i) = Range("z14")
vResults(4, i) = Range("r15")
End If
'resets values
Range("r16").Value = 13
Range("r14").Value = 0.7
Next i
Range("b96").Resize(4, c) = vResults
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
如果您希望在满足特定条件之前发生某些事情,但是您不知道发生的确切时间,则需要执行循环操作:
Do While Range("H83").Value < 23
Range("R14").Value = Range("R14").value + 0.01
Range("H83").value = Range("H83").value + x 'If you don't do something with Range("H83"), the loop will go on forever
Loop
编辑:犯过此错误的人不愿意承认:如果创建do循环,请确保不要创建无限循环,因为VBA不再响应的可能性很高