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

问题描述

我正在寻找一个do while循环,以在cellcert,4上输入一个数字,并在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,4).Value * (18 + (5 * (certcell - 2)))
    End If
    certcell = certcell + 1

Loop
End Sub
,

您在这里不需要IF语句。始终总是从18开始,然后将Column D (小于1)中的单元格值乘以5

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

您应该限定那些Cells对象或使用With块顺便说一句

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...