在VBA中自动填充

时间:2019-02-21 04:58:19

标签: excel vba

我是VBA的新手,请帮助。

我有使用if语句的代码,希望能够自动填充。这是下面的代码。

任何帮助将不胜感激。

Sub PaymentPriority()
    If Cells(3, 4) = "FED_ASS_DM" And Cells(3, 5) = "NOCHG" Then
        Cells(3, 33) = "999"
    Else
        Cells(3, 33) = "No Value"
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您要在AG3单元格中查找的公式是=IF(AND(D3="FED_ASS_DM",E3="NOCHG"),999,"No Value"),这就是您的VBA代码正在执行的操作。现在,您要做的就是找到最后一行,然后在1步内输入整个范围内的公式。

这是您要尝试的吗?

Sub PaymentPriority()
    Dim ws As Worksheet
    Dim sFormula As String

    '~~> Change this to the relevant sheet
    Set ws = Sheet1

    sFormula = "=IF(AND(D3=""FED_ASS_DM"",E3=""NOCHG""),999,""No Value"")"

    With ws
        '~~> Find last row
        lrow = .Range("D" & .Rows.Count).End(xlUp).Row

        '~~> Enter the formula in the entire range in 1 go
        .Range("AG3:AG" & lrow).Formula = sFormula
    End With
End Sub