Help w/ VBA formula, variables & errors

boznian

Board Regular
Joined
Mar 25, 2003
Messages
167
I am stuck again. I need to be able to set a formula with VB that contains variables for the row number. Every effort comes up with "Expected End of Statement" errors. Here is (one of ) the formula's that work fine if placed directly via Excel:

Code:
 =IF(OR(T323="",V323<>""),"",IF(MOD(TODAY()-T323,7)=0,"Renew",""))

I have been attempting things along these lines:

Code:
     Cells(crow, "U:U").Formula = "=" "IF(OR(("T:T", crow)="",("V:V", crow)<>""),"",IF(MOD(TODAY()-("T:T", crow),7)=0,"Renew",""))"

No matter how I slice and dice, can't get past multiple errors. btw, I am calling
Code:
     crow = ActiveCell.Row
earlier in the script and it works fine in all other parts of the script.

Another formula giving me the same fits:
Code:
 =IF(T322,IF(AND(T322>0,W322<>""),"",(TODAY()-T322)),"")

what am I doing wrong?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hello,

I am thinking this has to do with the quotes.

Try double quotes like:

Code:
"=IF(OR(T323="""",V323<>""""),"""",IF(MOD(TODAY()-T323,7)=0,""Renew"",""""))"
 
Upvote 0
Thanks for the reply, but you are working on the wrong formula. That one is the formula that works fine directly in Excel. I need to be able to place it via VB with variable row information. My attempts are similar to the second formula that I posted. I do believe you are correct that it is an issue with the quotes, but I don't know how to get around it.
 
Upvote 0
Not test but like this perhaps?:
Code:
"=IF(or(T" & crow & "="""",V" & crow & "<>""""),"""",IF(MOD(Today()-T" & crow & ",7)=0,""Renew"",""""))"
 
Last edited:
Upvote 0
To place a formula into a range(cell) in VBA use the .FormulaLocal or .FormulaR1C1 properties. It sounds like you want dynamic references so FormulaR1C1 will be more useful.

Easiest way to see how this works is to use the macro recorder and enter a formula manually, then go view the code.
 
Upvote 0
The second snip of code in the OP is lacking any concatenation operators (&). Putting them in should solve your problem.
 
Upvote 0
The second snip of code in the OP is lacking any concatenation operators (&). Putting them in should solve your problem.

Somehow I mistakenly took these out when writing the post - they were there in my original code and I still had the errors.

"Easiest way to see how this works is to use the macro recorder and enter a formula manually, then go view the code."

As silly a it sounds, I was completely unaware that this could be done and work in a "relative" fashion! The last time I tried this it worked one time, but never on the next row?? Maybe an old, old version of Excel? Anyway, got it working and opened many new possibilities, thanks
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top