Excel Escaping Single and double quotes

廉价感情. 提交于 2019-12-12 00:22:26

问题


I have a formula in each cell in excel where I need to edit. But I am having a hard time escaping the single quotes and double quotes using VBA code.

This is an example:

=+'F-222Alloc'!N2516+'F-222Alloc'!N2526

I need it to look like this

=+INDIRECT("'"&N14&"'!N2511")+INDIRECT("'"&N14&"'!N2526")

How do I use the REPLACE function properly?


回答1:


I find the easiest is to define a variable that contains just the double quote - then use it like any other string. Makes the code much more readable. Example:

Dim dq As String, sq as string
dq = Chr(34) ' double quote as a variable
sq = Chr(39) ' apostrophe or single quote as variable
Dim sourceString As String
sourceString = "hello"
msgbox sq + sourceString + "! " + dq + "you" + dq + sq

With these two variables you can create any string you want - after that, replacing what you want with something else (that might contain a crazy sequence of "'"'"'"("!"'") for all I care) becomes trivial.

Some helpful rules can be found in this article



来源:https://stackoverflow.com/questions/17430938/excel-escaping-single-and-double-quotes

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