简体   繁体   English

在VBA中自动填充

[英]Autofill in VBA

I am new to VBA and kindly need your help. 我是VBA的新手,请您提供帮助。

I have code that uses an if statement and would love to be able to autofill. 我有使用if语句的代码,希望能够自动填充。 Here is the code below. 这是下面的代码。

Any help will be much appreciated. 任何帮助都感激不尽。

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

The formula that you are looking for in cell AG3 is =IF(AND(D3="FED_ASS_DM",E3="NOCHG"),999,"No Value") which is what your VBA code is doing. 您在单元格AG3中寻找的公式是=IF(AND(D3="FED_ASS_DM",E3="NOCHG"),999,"No Value") ,这就是您的VBA代码正在执行的操作。 Now all you have to do is find the last row and enter the formula in the entire range in 1 go. 现在,您要做的就是找到最后一行,然后在1步内输入整个范围内的公式。

Is this what you are trying? 这是您要尝试的吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM