【发布时间】:2018-02-23 10:44:49
【问题描述】:
我正在编写一个函数,该函数遍历工作表的所有已使用单元格并用其他单元格替换字符(文件来自提取,并且有一些不需要的字符)。
这里是:
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 具有“框字符”,但它与 Excel 字符列表不对应(您可以在插入选项卡 -> 符号中找到它)。
有谁知道是否有办法在 VBA 代码中访问这些“框字符”?
【问题讨论】:
-
你试过 Cells.Replace What:="_",
-
这似乎是XY problem。为什么不正确地引入原始数据?
-
不是您特定问题的答案,因此请留下评论 -
Cells.Replace What:="Õ", Replacement:="ä", LookAt:=xlPart, MatchCase:=True将一次性完成工作表上的所有替换,而不是依次查看每个单元格。 编辑: 刚刚意识到这就是@QHarr 的意思。 :) -
将其中一个box字符放入单元格A1,在即时窗口中执行:
? AscW(Cells(1, 1)),您将得到box char的代码,例如。 G。对于╚,代码是 9562,那么您只需使用ChrW(9562)即可将该字符作为字符串。 -
正如 Jeeped 所说,我建议最好解决原因而不是尝试纠正症状。数据从何而来?尝试正确导入它,而不是稍后修复症状。