VBA Code Error

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
Hi, I have the following code that I am trying to insert in a specific cell via macro record:

=IF(AND(AA82="Both",IFERROR(YEAR(Q82),"A")=IFERROR(YEAR(D82),"B"),IFERROR(VLOOKUP(S82,Summary!A:B,2,FALSE),0)<>0),"DEL",IF(AND(AA82="Both",IFERROR(YEAR(Q82),"A")=IFERROR(YEAR(D82),"B"),IFERROR(VLOOKUP(S82,Summary!A:B,2,FALSE),1)=""),"TURN OFF LEASE - LEAVE ASSETS",IF(AND(AA82="Both",IFERROR(YEAR(Q82),"A")<>IFERROR(YEAR(D82),"B"),IFERROR(VLOOKUP(S82,Summary!A:B,2,FALSE),0)<>0),"TURN OFF ASSETS - LEAVE LEASE",IF(AND(AA82="CAP ONLY",IFERROR(VLOOKUP(S82,Summary!A:B,2,FALSE),0)<>0),"TURN OFF ASSETS",IF(AND(AA82="LEASE ONLY",IFERROR(YEAR(Q82),"A")=IFERROR(YEAR(D82),"B")),"TURN OFF LEASE","DO NOTHING")))))

It's a long formula, I know. However, when I select the cell and copy the formula, VBA records as the following:

ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""DEL"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),1)=""""),""TURN OFF LEASE - LEAVE ASSETS"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC["& _
"A"")<>IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS - LEAVE LEASE"",IF(AND(RC[-1]=""CAP ONLY"",IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS"",IF(AND(RC[-1]=""LEASE ONLY"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B"")),""TURN OFF LEASE"",""DO NOTHING"")))))""& _
"?????
It's inserting the question marks at the end I can't figure out why. Any suggestions? Thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
The macro recorder up until recent versions of excel just didnt work with long formulas. It cuts off 8 digits at each line feed from memory. See if this is better:

Code:
    Range("AB82").FormulaR1C1 = _
        "=IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""DEL"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),1)=""""),""TURN OFF LEASE - LEAVE ASSETS"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[" & _
        "-11]),""A"")<>IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS - LEAVE LEASE"",IF(AND(RC[-1]=""CAP ONLY"",IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS"",IF(AND(RC[-1]=""LEASE ONLY"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B"")),""TURN OFF LEASE"",""DO NOTHING"")))))"
 
Upvote 0
Looks like this worked. Will keep you updated if this is exactly what is needed, but thank you!!

The macro recorder up until recent versions of excel just didnt work with long formulas. It cuts off 8 digits at each line feed from memory. See if this is better:

Code:
    Range("AB82").FormulaR1C1 = _
        "=IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""DEL"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),1)=""""),""TURN OFF LEASE - LEAVE ASSETS"",IF(AND(RC[-1]=""Both"",IFERROR(YEAR(RC[" & _
        "-11]),""A"")<>IFERROR(YEAR(RC[-24]),""B""),IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS - LEAVE LEASE"",IF(AND(RC[-1]=""CAP ONLY"",IFERROR(VLOOKUP(RC[-9],Summary!C[-27]:C[-26],2,FALSE),0)<>0),""TURN OFF ASSETS"",IF(AND(RC[-1]=""LEASE ONLY"",IFERROR(YEAR(RC[-11]),""A"")=IFERROR(YEAR(RC[-24]),""B"")),""TURN OFF LEASE"",""DO NOTHING"")))))"
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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