VBA to enter formula that uses a variable that pulls sheet name as part of the formula... variable has a "-" in the name causing unexpected result

sshone1

New Member
Joined
Jun 14, 2013
Messages
10
Hello I have the following code that pulls a sheet name into a variable and then uses that variable (sheet name) to write a formula into another cell. It works great, unless the original sheet name has a "-" in it. It will then open a explorer window asking me for a file...? Anyone have an idea of why I cannot have a "-" in the name?

cName = copyWs.Range("A" & Y)

copyWs.Range("B" & Y).Formula = "=IF(" & cName & "!U5=0,""""," & cName & "!U5)"

Works great if cName = AT14495_01_ASM

In the cell I get the correct formula...
=IF(AT14495_01_ASM!U5=0,"",AT14495_01_ASM!U5)

However if cName = AT14495-01-ASM

I get a dialouge box titled Update Values:ASM and it wants me to select a file?

I know that sheet names can have "-" in them and you can write the formula manually and it works fine... Why is the script having this limitation?

Any help would be great
Thanks
Steve




 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try
VBA Code:
copyWs.Range("B" & Y).Formula = "=IF('" & cName & "'!U5=0,"""",'" & cName & "'!U5)"
 
Upvote 0
Solution
If a sheet name contains spaces or - then it needs to have an apostrophe front & back.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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