简体   繁体   English

强制命名范围引用文字字符串

[英]Force a named range to refer to a literal string

I'm using the following VBA code to reproduce the problem:我正在使用以下 VBA 代码来重现该问题:

ActiveWorkbook.Names.Add "Test", RefersTo:="1-2"

The resulting named range refers to "=42767" instead of ="1-2"生成的命名范围是指“=42767”而不是“1-2”

在此处输入图片说明

It seems that it is doing some calculation and setting RefersTo to the result.似乎它正在做一些计算并将 RefersTo 设置为结果。 How can I force the range to refer to the literal string "1-2"如何强制范围引用文字字符串“1-2”

Another example (although not as troublesome)另一个例子(虽然没那么麻烦)

ActiveWorkbook.Names.Add "Test", RefersTo:="123"

Results in RefersTo=123 instead of the desired RefersTo="123" (number vs text)结果是RefersTo=123而不是所需的RefersTo="123" (数字与文本)

Entering the RefersTo through the GUI with quotes works fine but doing it through VBA it gets altered通过带有引号的 GUI 输入 RefersTo 工作正常,但通过 VBA 执行它会被更改

The following code works以下代码有效

ActiveWorkbook.Names.Add "test", RefersTo:= "=" & Chr(34) & "1-2" & Chr(34)

You can replace "1-2" with any string variable or literal that you want to to be forced into the ranged name as text您可以将“1-2”替换为您想要强制作为文本输入范围名称的任何字符串变量或文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM