使用 If 语句执行 while 循环,在满足条件时添加一个值

Do while loop with If statement that adds a value when condition is met

我正在研究一个 do while 循环,以便在 cellcert, 4 上输入一个数字,并 return 在 cellcert,5 上输入一个值。但是,如果 cellcert, 4 值大于 1,它将为每个“1”加 5,因此它具有以下 If 逻辑:

1=18、2=18+5、3=18+5+5、4=18+5+5+5等等...

这就是为什么我正在考虑合并单独的函数来添加 5 个代码并在 Sub TATcomputation()

中调用它
Sub TATcomputation()

    Dim certcell As Integer
    certcell = 2

    Do While Cells(certcell, 4).Value <> ""
        Cells(certcell, 5).Value = Cells(certcell, 4).Value * 18
        certcell = certcell + 1
    Loop
End Sub

怎么样:

Sub TATcomputation()

Dim certcell As Integer
certcell = 2

Do While Cells(certcell, 4).Value <> ""
    
    If certcell = 2 Then
        Cells(certcell, 5).Value = Cells(certcell, 4).Value * 18
    Else
        Cells(certcell, 5).Value = Cells(certcell, 4).Value * (18 + (5 * (certcell - 2)))
    End If
    certcell = certcell + 1

Loop
End Sub

这里不需要 IF 声明。始终以 18 开头,然后将 Column D (减一) 中的单元格值乘以 5

'Do While....
    Cells(certvalue, 5).Value = 18 + ((Cells(certvalue, 4) -1) *5)
    'certvalue = cervalue + 1
'Loop

您应该限定那些 Cells 对象或使用 With 块 btw