Vba code to backup a cell formula - chr(34) does not work

benabrink

New Member
Joined
Nov 24, 2016
Messages
5
Hi there,


I am trying to backup a formula with a VBA code, i have already tried many combinations but nothing works.
Could you help me, idk what i'm doing wrong ...


Just to context, the entire process is to replace the formula (linked to 3 different sheets) replacing the "#REF" caused by delete these sheets and import them again.

Formula:

=IF(IF(OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1);VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&"_"&$G1;Padrao!$A:$E;5;FALSE))="MR";VLOOKUP($G1;Up_lw!$C:$E;3;FALSE);IF(OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1);VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&"_"&$G1;Padrao!$A:$E;5;FALSE)))

if i try to use the code bellow, the VB returns an error as "Application-define or object-define error"

teste = "=IF(IF(OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1);VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&'_'&$G1;Padrao!$A:$E;5;FALSE))='MR';VLOOKUP($G1;Up_lw!$C:$E;3;FALSE);IF(OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1);VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&'_'&$G1;Padrao!$A:$E;5;FALSE)))"

Range("form_cluster_unico").Formula = Replace(teste, "'", Chr(34))

Regards
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You've replaced quote marks with apostrophe's when you should have used double quotes. Do you still get the error if you replace
'Mr'
with
""Mr""

Also, you could condense your formula so that this:
PHP:
OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;  201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1)

becomes this:
PHP:
OR(AND($C1>=201501,$C1<=201512),AND($C1>=201601,$C1<=201603))
 
Upvote 0
You've replaced quote marks with apostrophe's when you should have used double quotes. Do you still get the error if you replace
'Mr'
with
""Mr""

Also, you could condense your formula so that this:
PHP:
OR(201501=$C1;201502=$C1;201503=$C1;201504=$C1;201505=$C1;201506=$C1;201507=$C1;201508=$C1;201509=$C1;201510=$C1;  201511=$C1;201512=$C1;201601=$C1;201602=$C1;201603=$C1)

becomes this:
PHP:
OR(AND($C1>=201501,$C1<=201512),AND($C1>=201601,$C1<=201603))

njimack, ty for the sugestion !!
i´ll condense it, but about the use of double quotes, it still not working
 
Upvote 0
njimack, ty for the sugestion !!
i´ll condense it, but about the use of double quotes, it still not working
now the formula is:

Code:
=IF(IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&"_"&$G1;Padrao!$A:$E;5;FALSE))="MR";VLOOKUP($G1;Up_lw!$C:$E;3;FALSE);IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&"_"&$G1;Padrao!$A:$E;5;FALSE)))

But it does not work in VB Code (When i use the syntax "Range("XYZ").Formula = code above"
 
Upvote 0
You need to replace every apostrophe within the formula, not just the example I gave.
You are saying to do like this:

Range("XYZ").Formula = "=IF(IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&""_""&$G1;Padrao!$A:$E;5;FALSE))=""MR"";VLOOKUP($G1;Up_lw!$C:$E;3;FALSE);IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&""_""&$G1;Padrao!$A:$E;5;FALSE)))"

right ?
but it does not work
 
Upvote 0
I can't test this because I have different regional settings to you, but the below should work:

Code:
Range("A1").Formula = "=IF(IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&""_""&$G1;Padrao!$A:$E;5;FALSE))=""MR"";VLOOKUP($G1;Up_lw!$C:$E;3;FALSE);IF(OR(AND($C1>=201501;$C1<=201512);AND($C1>=201601;$C1<=201603));VLOOKUP($G1;mar!$C:$E;3;FALSE);VLOOKUP($C1&""_""&$G1;Padrao!$A:$E;5;FALSE)))"
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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