问题
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