vba 输入 '=' 作为单元格中的第一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6458941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:32:48  来源:igfitidea点击:

Entering '=' as the first character in a cell

excelvbaexcel-vba

提问by Logan

I have a VBA macro that runs and often needs to have "=" as the first character in a cell. Cells are filled with such values as "= Domestic", "<> Domestic", etc.

我有一个运行的 VBA 宏,通常需要将“=”作为单元格中的第一个字符。单元格填充有“=国内”、“<>国内”等值。

Right now I am replacing "=" with "IS" as a quickfix, but I would like it if I could keep the equal sign, for consistency with cells that use symbols such as "<>". When I try, Excel thinks it is a function with the wrong syntax. I am using Cells(row, col).Value = x, where xis the string that may or may not start with "=", to enter the data.

现在我正在用“IS”替换“=”作为快速修复,但如果我可以保留等号,我希望它与使用“<>”等符号的单元格保持一致。当我尝试时,Excel 认为它是一个语法错误的函数。我正在使用以“=”开头或不以“=”开头的字符串Cells(row, col).Value = x在哪里x输入数据。

回答by manji

Add a single quote 'at the start of the string:

'在字符串的开头添加单引号:

Cells(row, col).Value = "'" & x

and you will get what you want.

你会得到你想要的。

回答by ray

This is an alternate method

这是另一种方法

Sub Test()
    Dim myRange As Range

    Set myRange = Range("C:C") 'Column "C"
    myRange.NumberFormat = "@" 'Set cell format of range to plain text
    myRange.Cells(1, 1) = "= Domestic"

End Sub

Compared to manji's answer, it provides no performance or size enhancement...just different.

与 manji 的答案相比,它没有提供性能或尺寸增强......只是不同。