简体   繁体   中英

Filling columns with formula based on another Cell

I am currently sending my formula to column K & L based on a user form. It sends the formula based on Column G's input. I want to send the formula to columns K & L only if G has a value in it. Can any one help me?

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

If you mean only run the code if G has at least one non-blank cell you could use this. You can apply the formulae across the whole range in one go.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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