使用 VBA (excel) 将特殊的 ascii 字符插入到文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14225325/
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
Inserting special ascii characters into a file using VBA (excel)
提问by Mike Williamson
I am fairly new to VBA, but not to coding in general. I have an Excel worksheet, part of which I need to export into a text file. However, to ensure the escape characters don't collide with anything written in the worksheet, and also to match up with how escape characters are handled in other (Java & C) apps at our company, I would like to use the ascii character "thorn".
Some languages that I know, like perl, will simply let you enter an escape character and the ascii number of the symbol. E.g., I could type "\231" in order to get "thorn". It seems this is not supported in VBA. Or if it is, I am not sure how.
Lastly, below is a brief snippet of how I imagine I would implement the code, with just the key statements. If you have a better suggestion, or know why this wouldn't be the best way, please let me know.
我对 VBA 相当陌生,但对一般的编码并不陌生。我有一个 Excel 工作表,我需要将其中的一部分导出到文本文件中。但是,为了确保转义字符不会与工作表中写入的任何内容发生冲突,并与我们公司其他(Java 和 C)应用程序中处理转义字符的方式相匹配,我想使用 ascii 字符“刺”。
我知道的一些语言,比如 perl,只会让你输入一个转义字符和符号的 ascii 数字。例如,我可以输入“\231”以获得“ thorn”。VBA 中似乎不支持此功能。或者,如果是,我不确定如何。
最后,下面是我想象我将如何实现代码的简短片段,仅包含关键语句。如果您有更好的建议,或者知道为什么这不是最好的方法,请告诉我。
Open sFullPath For Output As #1
(... some code to capture the values to be written to the next line of the file ...)
(...一些代码来捕获要写入文件下一行的值...)
sLine = Join(sValues, <the thorn character>)
Print #1, sLine
The code above would be placed within a loop that would regenerate "sValues", the set of relevant cells in the next row to be printed to the text file.
Thanks so much!
Regards,
Mike
上面的代码将放置在一个循环中,该循环将重新生成“sValues”,即下一行中要打印到文本文件中的一组相关单元格。
非常感谢!
问候,
迈克
采纳答案by Nick Perkins
To print an ASCII character using its code number, use the chr()
function
要使用其代码编号打印 ASCII 字符,请使用chr()
函数
Extended ASCII isn't standard across all platforms. For whatever reason, the number needed is 222.
扩展 ASCII 不是所有平台的标准。无论出于何种原因,所需的数字都是 222。
So you would need
所以你需要
sLine = Join(sValues, chr(222))
Print #1, sLine
回答by mkingston
Try replacing <the thorn character>
with Chr(254)
. Let me know how that goes.
尝试替换<the thorn character>
为Chr(254)
. 让我知道这是怎么回事。