formula in VBA

D

Dave

Is the Friday afternoon?
I'm trying to create a formula in VBA. The formula is:
=IF(I3<>"",IF(J3<>"",SUM(J3:L3)*I3+M3,""),"")
I've written the code, but VBA is not accepting it. So far I have:
..Cells(lNextRow, "N").Formula = "=IF(I" & lNextRow & "<>"""","IF(J" &
lNextRow & "<>"""","SUM(J" & lNextRow & ":L" & lNextRow & ")*I" & lnextrow
&"+M" & lnextrow & ",""""),"""")"

But it's in red. I can't see what's wrong. Does anyone see it?
 
T

Tim Zych

..Cells(lNextRow, "N").Formula = _
"=IF(I" & lnextrow & "<>"""",IF(J" & lnextrow & _
"<>"""",SUM(J" & lnextrow & ":L" & lnextrow & _
")*I" & lnextrow & "+M" & lnextrow & ",""""),"""")"

Another way to embed double-quotes is to use a variable or constant. Can be
easier to write and debug, IMO.

Const DQ As String = """"

..Cells(lNextRow, "N").Formula = _
"=IF(I" & lnextrow & "<>" & DQ & DQ & _
",IF(J" & lnextrow & "<>" & DQ & DQ & _
",SUM(J" & lnextrow & ":L" & lnextrow & ")*I" & _
lnextrow & "+M" & lnextrow & "," & DQ & DQ & ")," & DQ & DQ & ")"
 
S

salgud

.Cells(lNextRow, "N").Formula = _
"=IF(I" & lnextrow & "<>"""",IF(J" & lnextrow & _
"<>"""",SUM(J" & lnextrow & ":L" & lnextrow & _
")*I" & lnextrow & "+M" & lnextrow & ",""""),"""")"

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top