【问题标题】:Find Replace of Greek characters in Excel using VBA macro使用 VBA 宏在 Excel 中查找替换希腊字符
【发布时间】:2020-06-11 16:57:08
【问题描述】:

我正在尝试制作一个 VBA 宏来帮助我清理 Excel 材料清单中的电子零件描述。它使用 Trim 删除多余的空格并缩短几个长单词。但是,我不知道如何正确查找/替换像欧米茄这样的希腊符号。字符代码数字似乎不起作用。 我不确定我是否使用了 ChrW 错误或问题所在。

在另一个论坛上,有人建议我需要将 MatchByte:=True 添加到代码中,但这似乎没有帮助或阻碍。

通过大量实验,我发现了解决部分问题的方法。事实证明,当您将其符号粘贴到 VBA 查找/替换代码中时,µ - MICRO SIGN(Unicode 和 ASCII(十六进制)字符代码 00B5 以及 ASCII(十进制)字符代码 181)工作得非常好。宏然后完美地找到/替换它。

困惑为什么希腊的 mu、omega 和 rho 符号不能这样工作。

μ - 希腊小写字母 MU(Unicode(十六进制)字符代码 03BC)

Ω - 希腊大写字母 OMEGA(Unicode(十六进制)字符代码 03A9)

Ω - OHM 符号(Unicode(十六进制)字符代码 2126)

ρ - 希腊小写字母 RHO(Unicode(十六进制)字符代码 03C1)

Sub DescriptionFix()
'
' DescriptionFix Macro
' Shortens Description by replacing long words and removes excess spaces from the Description Column
' Select a column or a portion of a column to run this macro on
'
    Dim r As Range

    With Application.WorksheetFunction
    For Each r In Intersect(Selection, ActiveSheet.UsedRange)
        r.Value = .Trim(r.Value)
    Next r
    Selection.Replace What:="RESISTOR", Replacement:="RES", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Selection.Replace What:="ChrW(2126)", Replacement:="OHM", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, MatchByte:=True, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="ChrW(03A9)", Replacement:="OHM", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Selection.Replace What:="TRANSISTOR", Replacement:="TRANS", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="CRYSTAL", Replacement:="XTAL", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="CAPACITOR", Replacement:="CAP", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="µ", Replacement:="u", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Selection.Replace What:="ChrW(03BC)", Replacement:="u", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        MatchByte:=True, ReplaceFormat:=False
    Selection.Replace What:="ChrW(03C1)", Replacement:="p", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        MatchByte:=True, ReplaceFormat:=False

    Selection.Replace What:="TANTALUM", Replacement:="TANT", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="CERAMIC", Replacement:="CER", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="INDUCTOR", Replacement:="IND", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="FERRITE", Replacement:="FERR", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="GREEN", Replacement:="GRN", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="BLACK", Replacement:="BLK", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="YELLOW", Replacement:="YEL", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="VOLTAGE", Replacement:="VOLT", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="REGULATOR", Replacement:="REG", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="CONNECTOR", Replacement:="CONN", LookAt:=xlPart _
        , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Selection.Replace What:="TRANSFORMER", Replacement:="XFMR", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With
End Sub

【问题讨论】:

  • 不要在字符代码周围使用引号(即使用 ChrW(03C1) 而不是 "ChrW(03C1)" 否则您实际上是在寻找 ChrW(03C1) 而不是它所代表的字符
  • @cybernetic.nomad 我试过了 - Selection.Replace What:=ChrW(03A9), Replacement:="OHM", LookAt:= _ xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat :=False, _ ReplaceFormat:=False 但我收到一条错误消息“编译错误:预期:列表分隔符或)”,这似乎是某种不正确的语法错误。
  • 语法错误可能是拼写错误的一部分。请记住,您可能仍然需要使用 MatchByte,正如您在此处的 vbforums 问题中所建议的那样 vbforums.com/…
  • 尝试使用十进制而不是十六进制值ChrW(937) 而不是ChrW(03A9)
  • @cybernetic.nomad 哦,哇,这完全有效。我在哪里查找这些代码?当我选择 Omega 时,插入 > 符号图表中没有显示 937?如果我选择它然后选择“ASCII(十进制)”它就会消失。

标签: excel replace unicode find character


【解决方案1】:
  1. 不要在字符代码周围使用引号(即使用 ChrW(03C1) 而不是 "ChrW(03C1)" 否则您实际上是在寻找字符串 ChrW(03C1) 而不是它所代表的字符

  2. VBA 期望代码是十进制而不是十六进制(即ChrW(937) 而不是ChrW(03A9)

  3. 如果您需要将代码从十六进制转换为十进制,您可以使用任何一种转换器或在线查找表。类似https://unicodelookup.com/

【讨论】:

  • 感谢您的帮助!! ChrW(8486) - 欧姆符号 - 和 ChrW(937) - 大写欧米茄 - 两者都有效,但奇怪的是,它们都删除了所有欧米茄符号,即使一个是欧姆符号而另一个是大写欧米茄。我非常感谢 Unicode 查找网站链接,它帮助了我:)
  • ChrW(&H03C1) 应该适用于十六进制常量。
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 2023-03-17
  • 2013-07-15
相关资源
最近更新 更多