gpt4 book ai didi

vba - 如何替换 Excel 工作表中的方框字符?

转载 作者:行者123 更新时间:2023-12-04 20:10:40 24 4
gpt4 key购买 nike

我正在编写一个函数,该函数遍历工作表的所有已使用单元格并用其他字符替换字符(文件来自提取,并且有一些不需要的字符)。

这里是 :

Sub replaceSpecialCharacters()
Dim Cell As Range

For Each Cell In ActiveSheet.UsedRange
Cell.Value = Replace(Cell.Value, "Õ", "ä")
Cell.Value = Replace(Cell.Value, "³", "ü")
Cell.Value = Replace(Cell.Value, "÷", "ö")
Cell.Value = Replace(Cell.Value, Chr(111), "Ä")
Cell.Value = Replace(Cell.Value, Chr(220), "Ü")
Cell.Value = Replace(Cell.Value, "Í", "Ö")
Cell.Value = Replace(Cell.Value, "Ó", "à")
Cell.Value = Replace(Cell.Value, "Ú", "é")
Cell.Value = Replace(Cell.Value, "Þ", "è")
Cell.Value = Replace(Cell.Value, "Ô", "â")
Cell.Value = Replace(Cell.Value, "¶", "ô")
Cell.Value = Replace(Cell.Value, Chr(194), "Â")
Cell.Value = Replace(Cell.Value, Chr(200), "È")
Cell.Value = Replace(Cell.Value, Chr(202), "Ê")
Cell.Value = Replace(Cell.Value, "Ù", "œ")
Next
End Sub

它有点慢,但效果很好。我的问题是我找不到替换 Ascii“框字符”(▄、┬、╚ 和╩对于我的情况)的方法。

我尝试使用 Chr() 函数将 Ascii 字符放入 vba 代码中,但 Excel Ascii 字符集似乎没有我正在寻找的那些。

在互联网上搜索 Ascii 字符代码时,我发现了 this list其中有“框字符”,但它不对应于 Ascii 字符的 Excel 列表(您可以在插入选项卡 -> 符号中找到它)。

有谁知道是否有办法在 VBA 代码中访问这些“框字符”?

最佳答案

下面的代码会将 A1 的登录代码放入 A2

Cells(2,1).Value = AscW(Cells(1,1).Value)
对于▄,这将是 9604。
您可以在替换中使用此代码,如下所示:
Cell.Value = Replace(Cell.Value, ChrW(9604), "Â")

编辑:来自@Tom Blodget 的一些信息形成了评论:

It is important to understand that VBA uses the UTF-16 encoding ofthe Unicode character set. AscW and ChrW deal with one UTF-16 codeunit.

(Asc and Chr cause transcoding between UTF-16 and the user'sANSI character encoding at the time the code executes, not ASCII.)

关于vba - 如何替换 Excel 工作表中的方框字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48945981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com