Inserting a formula containing an ampersand into a cell

我的未来我决定 提交于 2020-07-19 19:02:05

问题


I'm trying to create a formula in the A1 cell which contains an ampersand character that looks something like this in Excel:

="Hello, "&A2

My VBA code is as follows:

Worksheets("Sheet1").Range("A1").Formula = "=""Hello, "" & "&A2"

VBA is throwing errors, and I can't figure out why. I've tried using double ampersands, and double quotes in various places and can't get around this error.


回答1:


Consider:

Sub dural()
   Range("A1").Formula = "=" & Chr(34) & "Hello" & Chr(34) & "& A2"
End Sub



回答2:


Please consider this pithy line:

[a1] = "=""Hello, "" & A2"

...which will evaluate to, "Hello, Dave"




回答3:


You have one extra ":

Worksheets("Sheet1").Range("A1").Formula = "=""Hello, "" & A2"



回答4:


Use the & character code of 38:

Worksheets("Sheet1").Range("A1").Formula = "=" & Chr(34) & "Hello, " & Chr(34) & Chr(38) & "A2"



回答5:


This should work as well.

 ActiveCell.FormulaR1C1 = "=""hello "" & A2"


来源:https://stackoverflow.com/questions/33291221/inserting-a-formula-containing-an-ampersand-into-a-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!