Indirect formula VBA

Partjob

Board Regular
Joined
Apr 17, 2008
Messages
139
Hi
I have a problem using the function Indirect within VBA. I am trying to insert the formula below using VBA. I know why I have the problem. The apostrophes are causing the formula to comment out. I don't know how to get around this if you can at all.
I have tried replacing the apostrophe with a different character to then find and replace later, I could not get this to work. the sheet name in in Cell ("C4") and I then reference it in the formula to get the result.
There does not seem to be indirect within VBA.
I don't really know another way I can return the result using adifferent method.
Code:
Sheet1.Range("E7").Formula = "=IF(C4="",TODAY()-1,INDEX(INDIRECT("'" & C40 & "'!$A$5:$BC$43"),MATCH(A4,INDIRECT("'" & C4 & "'!$A$5:$A$43"),0),49))"
Thanks as usual for your help, this seems simple but I been pondering this for over a day now and need direction.
Partjob
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This will work, ie the code won't error, but I've no idea if it'll produce the formula you want.
Code:
Sheet1.Range("E7").Formula = "=IF(C4="""",TODAY()-1,INDEX(INDIRECT(""'"" & C40 & ""'!$A$5:$BC$43""),MATCH(A4,INDIRECT( ""'"" & C40 & ""'!$A$5:$A$43""),0),49))"
The formula that is in E7 looks like this.

=IF(C4="",TODAY()-1,INDEX(INDIRECT("'" & C40 & "'!$A$5:$BC$43"),MATCH(A4,INDIRECT( "'" & C40 & "'!$A$5:$A$43"),0),49))
 
Upvote 0
Sheet1.Range("E7").Formula = replace("=IF(C4=##,TODAY()-1,INDEX(INDIRECT(' & C40 & '!$A$5:$BC$43#),MATCH(A4,INDIRECT(' & C4 & '!$A$5:$A$43#),0),49))","#",chr(34))
 
Upvote 0
Hi

To write a double quote inside a double quoted string you have to double it. So, after:

Code:
s= "This ""is"" as test"

s has the value

This "is" as test

In your case you are missing a lot of double quotes. I think you want:

Code:
Sheet1.Range("E7").Formula = "=IF(C4="""",TODAY()-1,INDEX(INDIRECT(""'" & C40 & "'!$A$5:$BC$43""),MATCH(A4,INDIRECT(""'" & C4 & "'!$A$5:$A$43""),0),49))"

For C40 = "Sheet2" and C4 = "Sheet3", you get

=IF(C4="",TODAY()-1,INDEX(INDIRECT("'Sheet2'!$A$5:$BC$43"),MATCH(A4,INDIRECT("'Sheet3'!$A$5:$A$43"),0),49))
 
Upvote 0
Norrie
Not for the first time you have got me out of a jam, you were correct about the function not working but I have solved that, I did know about double quoteing. So simple really.
Thank you
Partjob
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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