Help! Formula assigned to a variable yields unwanted quotation marks.

K

keithb

I need to assign a name to a formula. Using the code shown below, the
resulting named formula is surrounded by quotation marks and does not
execute properly when the name is used in a cell reference. Can someone
suggest an alternate syntax that will name the formula without adding
quotation marks?

sFormula = "OFFSET(Sheet1!$E$1,1,0,COUNTA(Sheet1!$E:$E)-1)"
ActiveWorkbook.Names.Add Name:="thispath", RefersToR1C1:=sFormula

Thanks,

Keith
 
M

Max

Perhaps try instead:

sFormula = "=OFFSET(Sheet1!R1C5,1,0,COUNTA(Sheet1!C5)-1)"
ActiveWorkbook.Names.Add Name:="thispath", RefersToR1C1:= _
sFormula
 
K

keithb

Thanks for your suggestion; however, that gives me an "error in formula"
message.
 
H

Herbert Seidenberg

I can duplicate your problem if I enter
OFFSET(Sheet1!$E$1,1,0,COUNTA(Sheet1!$E:$E)-1)
into the RefersTo box instead of
=OFFSET(Sheet1!$E$1,1,0,COUNTA(Sheet1!$E:$E)-1)
 
K

keithb

Thanks Max, your solution really saved me!

I do have one question if you don't mind:
Your solution uses a different syntax (R!C5 Vs. $E$1) as compared to the
result viewed in the Names dialog: This is your formula:

sFormula = "=OFFSET(Sheet1!R1C5,1,0,COUNTA(Sheet1!C5)-1)"

This is what gets displayed in the Define Name dialog box after clicking
your command button:

=OFFSET(Sheet1!$E$1,1,0,COUNTA(Sheet1!$E:$E)-1)

So how did you know to use the R1C5 designation to create a $E$1 in the
result?

Many thanks,

Keith
 
M

Max

Glad to hear you got it going !
So how did you know to use the R1C5 designation
to create a $E$1 in the result?
sFormula = "=OFFSET(Sheet1!R1C5,1,0,COUNTA(Sheet1!C5)-1)"

Aha, I did cheat a little <g>. Tinkered around in reverse. Recorded a macro
to create the name (via Insert>Name Define) and pasted in the dynamic range
formula from your post. The equivalent formula in R1C1 style was an output
from the macro recorder.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top