使用基于另一个单元格的公式填充列

时间:2018-12-20 15:52:52

标签: excel vba

我当前正在根据用户表单将公式发送到K&L列。它根据列G的输入发送公式。我想仅在G中包含值的情况下才将公式发送到K和L列。有人可以帮我吗?

picture of datasheet

    'Sets Columns K & L to Method 6 Formula and looks for last populated row in the Nominal Column (G)
    LastRow = Range("G" & Rows.Count).End(xlUp).Row
    Range("K7").Formula = "=M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"
    Range("L7").Formula = "=N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"

    If LastRow = 7 Then
    Else
    Range("K7").AutoFill Destination:=Range("K7:K" & LastRow)
    Range("L7").AutoFill Destination:=Range("L7:L" & LastRow)

    End If

1 个答案:

答案 0 :(得分:0)

如果您的意思是仅在G具有至少一个非空白单元格的情况下运行代码,则可以使用此代码。您可以一次将公式应用于整个范围。

Sub x()

Dim LastRow As Long

LastRow = Range("G" & Rows.Count).End(xlUp).Row

Range("K7:K" & LastRow).Formula = "=IF(G7="""", """",M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"
Range("L7:L" & LastRow).Formula = "=IF(G7="""", """",N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"

End Sub
相关问题