我正在尝试像这样在C列中填充公式:

LastRow = Range("A65536").End(xlUp).Row
Range(Cells(1, 3), Cells(LastRow, 3)).Formula = "=A1*B1"


而且效果很好。但是是否可以跳过列C中的那些单元格,例如> 0?

A B C -> A B C
3 2 0    3 2 6
3 4 0    3 4 12
5 6 5    5 6 5  <- this value is not updated

最佳答案

一种通过循环执行此操作的方法:

    Sub test()

            Dim rngTest As Range
            Dim rngCell As Range

            LastRow = Range("A65536").End(xlUp).Row
            Set rngTest = Range(Cells(1, 3), Cells(LastRow, 3))
            Application.Calculation = xlCalculationManual
            For Each rngCell In rngTest.Cells
                If Not rngCell <> "" Then
                    rngCell.Formula = "=" & rngCell.Offset(, -2).Address & "*" & rngCell.Offset(, -1).Address
                End If
            Next
            Application.Calculation = xlCalculationAutomatic
    End Sub

10-04 15:45