【问题标题】:Removing special characters VBA Excel删除特殊字符 VBA Excel
【发布时间】:2014-06-23 00:23:15
【问题描述】:

我正在使用 VBA 阅读一些 TITLES,然后将该信息复制到 powerpoint 演示文稿中。

我的问题是,标题有特殊字符,但我也处理的图像文件没有。

TITLE 构成了将 JPEG 加载到图片容器的路径的一部分。例如。 “P k.jpg”,但标题叫“p.k”。

我希望能够忽略 TITLE 中的特殊字符,而是让它看到一个空格,以便它选择正确的 JPG 文件。

这可能吗?

谢谢!

【问题讨论】:

    标签: vba excel excel-2010


    【解决方案1】:

    您认为什么是“特殊”字符,只是简单的标点符号?您应该能够使用Replace 函数:Replace("p.k","."," ")

    Sub Test()
    Dim myString as String
    Dim newString as String
    
    myString = "p.k"
    
    newString = replace(myString, ".", " ")
    
    MsgBox newString
    
    End Sub
    

    如果你有多个字符,你可以在自定义函数或Replace函数的简单链式系列等中做到这一点。

      Sub Test()
    Dim myString as String
    Dim newString as String
    
    myString = "!p.k"
    
    newString = Replace(Replace(myString, ".", " "), "!", " ")
    
    '## OR, if it is easier for you to interpret, you can do two sequential statements:
    'newString = replace(myString, ".", " ")
    'newString = replace(newString, "!", " ")
    
    MsgBox newString
    
    End Sub
    

    如果您有很多潜在的特殊字符(例如非英语重音 ascii?),您可以对数组执行自定义函数或迭代。

    Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?"  'modify as needed
    Sub test()
    Dim myString as String
    Dim newString as String
    Dim char as Variant
    myString = "!p#*@)k{kdfhouef3829J"
    newString = myString
    For each char in Split(SpecialCharacters, ",")
        newString = Replace(newString, char, " ")
    Next
    End Sub
    

    【讨论】:

    • 很棒的帖子。但这对角色不起作用?
    • @JohnAndrews 是的,如果您修改 SpecialCharacters 字符串以便问号是其中的一部分:Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?"
    • 我认为列表中唯一缺少的字符是逗号本身!但我知道要针对该字符进行调整需要一些额外的修改
    • 若要同时删除逗号,请将第一行替换为 Const SpecialCharacters As String = "! @ # $ % ^ & * ( ) { [ ] } ? ," 和 for 语句中的 For each char in Split(SpecialCharacters, " ")。您可以将两个语句中的任何其他字符替换为空格,例如_
    • 任何用“_”替换空格的提示?我在字符列表中添加了一个空格,但由于某种原因它不起作用
    【解决方案2】:

    如果您不仅要排除特殊字符列表,而且要排除 所有 不是字母或数字的字符,我建议您使用 char 类型比较方法。

    对于字符串中的每个字符,我会检查 unicode 字符是否介于“A”和“Z”之间、“a”和“z”之间或“0”和“9”之间。这是vba代码:

    Function cleanString(text As String) As String
        Dim output As String
        Dim c 'since char type does not exist in vba, we have to use variant type.
        For i = 1 To Len(text)
            c = Mid(text, i, 1) 'Select the character at the i position
            If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z") Then
                output = output & c 'add the character to your output.
            Else
                output = output & " " 'add the replacement character (space) to your output
            End If
        Next
        cleanString = output
    End Function
    

    如果您想进一步自定义此功能,Wikipedia list of Unicode characers 是一个很好的快速入门。

    即使用户找到引入新特殊字符的方法,此解决方案也具有实用性的优势。它也比比较两个列表更快。

    【讨论】:

    • 如果单元格中的字符数很少,这比正则表达式快得多。对于单元格有 20 个字符,大约快 15 倍。大约 2000 个字符时,它们大致相同,正则表达式越多,速度越快。
    【解决方案3】:

    这是删除特殊字符的方法。

    我只是应用了正则表达式

    Dim strPattern As String: strPattern = "[^a-zA-Z0-9]" 'The regex pattern to find special characters
    Dim strReplace As String: strReplace = "" 'The replacement for the special characters
    Set regEx = CreateObject("vbscript.regexp") 'Initialize the regex object    
    Dim GCID As String: GCID = "Text #N/A" 'The text to be stripped of special characters
    
    ' Configure the regex object
    With regEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = strPattern
    End With
    
    ' Perform the regex replacement
    GCID = regEx.Replace(GCID, strReplace)
    

    【讨论】:

    【解决方案4】:

    这是我使用的,基于link

    Function StripAccentb(RA As Range)
    
    Dim A As String * 1
    Dim B As String * 1
    Dim i As Integer
    Dim S As String
    'Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
    'Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
    Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
    Const RegChars = "neuaicoeooa"
    S = RA.Cells.Text
    For i = 1 To Len(AccChars)
    A = Mid(AccChars, i, 1)
    B = Mid(RegChars, i, 1)
    S = Replace(S, A, B)
    'Debug.Print (S)
    Next
    
    
    StripAccentb = S
    
    Exit Function
    End Function
    

    用法:

    =StripAccentb(B2) ' cell address
    

    工作表中所有单元格的子版本:

    Sub replacesub()
    Dim A As String * 1
    Dim B As String * 1
    Dim i As Integer
    Dim S As String
    Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
    Const RegChars = "neuaicoeooa"
    Range("A1").Resize(Cells.Find(what:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
    Cells.Find(what:="*", SearchOrder:=xlByColumns, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select '
    For Each cell In Selection
    If cell <> "" Then
    S = cell.Text
        For i = 1 To Len(AccChars)
        A = Mid(AccChars, i, 1)
        B = Mid(RegChars, i, 1)
        S = replace(S, A, B)
        Next
    cell.Value = S
    Debug.Print "celltext "; (cell.Text)
    End If
    Next cell
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多