[英]Change cell value loop in VBA
What I want this part of the code to do is if the value of cell H83 is greater than 22 then I want the code to increase the value of cell R14 which is 0.7 by an increment of 0.01 until either R14 reaches 0.75 or until H83 is less than 22. I have tried: 我希望代码的这一部分要做的是,如果单元格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
This doesn't work and right now I have it so it increases it by 0.01 only once (part in asterisks), full code: 这是行不通的,现在我有了它,因此它仅将它一次增加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
If you want something to happen until a certain condition is met, but you don't know exactly when that happens, you need a Do-Loop: 如果您希望在满足特定条件之前发生某些事情,但是您不知道确切的发生时间,则需要执行“循环”操作:
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
Edit: From someone who made this mistake more than willing to admit: If you create a do-loop, please make sure that you don't create an infinite loop, because the chance is high that VBA won't respond anymore. 编辑:犯了这个错误的人不愿意承认:如果创建了do-loop,请确保不要创建无限循环,因为VBA不再响应的可能性很高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.