从 VBA 中的文本中剥离特殊字符的问题
Issues stripping special characters from text in VBA
我有一个 Excel 文件,它从 csv 文件中提取数据,对其进行一些操作,然后将其保存为一系列文本文件。
源数据中有一些特殊字符会出错,所以我添加了这个来去除它们
Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?,â,€,™"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String
Dim char As Variant
newString = myString
For Each char In Split(SpecialCharacters, ",")
newString = Replace(newString, char, "")
Next
ReplaceSpecialCharacters = newString
End Function
问题是这并没有捕捉到所有这些。当我尝试处理以下文本时,它会跳过上面的代码并导致 Excel 出错。
Hero’s Village
我认为问题在于特殊字符未被 Excel 本身识别。我只能通过将文本从 Excel 复制出来并将其粘贴到另一个 IDE 来使文本看起来像上面那样。在 Excel 中显示为:
在工作簿中
在编辑区
立即window
基于 this site 看起来它在显示 '
字符时出现问题,但是如果它甚至无法正确读取它,我该如何将其显示为 fix/filter在 VBA 本身?
Option Explicit
dim mystring as String
dim regex as new RegExp
Private Function rgclean(ByVal mystring As String) As String
'function that find and replace string if contains regex pattern
'returns str
With regex
.Global = True
.Pattern = "[^ \w]" 'regex pattern will ignore spaces, word and number characters...
End With
rgclean = regex.Replace(mystring, "") '.. and replaces everything else with ""
End Function
尝试使用正则表达式。
确保您启用了正则表达式:
工具 > 参考 > 复选框:“Microsoft VBScript 正则表达式 5.5”
将“mystring”字符串变量传递给函数 (rgclean)。该函数将检查任何不是 space、单词 [A-Za-z] 或数字 [0-9] 的内容,将它们替换为“”,以及 returns 字符串。
该函数几乎可以删除字符串中的所有符号。不排除任何数字、Space 或单词。
这里是相反的做法。删除不包含在这组 62 中的所有字符:
ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
代码:
Const ValidCharacters As String = "ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String, L As Long, i As Long
Dim char As Variant
newString = myString
L = Len(newString)
For i = 1 To L
char = Mid(newString, i, 1)
If InStr(ValidCharacters, char) = 0 Then
newString = Replace(newString, char, "@")
End If
Next i
ReplaceSpecialCharacters = Replace(newString, "@", "")
End Function
注:
如果要保留字符,也可以向字符串 ValidCharacters
添加字符。
我有一个 Excel 文件,它从 csv 文件中提取数据,对其进行一些操作,然后将其保存为一系列文本文件。
源数据中有一些特殊字符会出错,所以我添加了这个来去除它们
Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?,â,€,™"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String
Dim char As Variant
newString = myString
For Each char In Split(SpecialCharacters, ",")
newString = Replace(newString, char, "")
Next
ReplaceSpecialCharacters = newString
End Function
问题是这并没有捕捉到所有这些。当我尝试处理以下文本时,它会跳过上面的代码并导致 Excel 出错。
Hero’s Village
我认为问题在于特殊字符未被 Excel 本身识别。我只能通过将文本从 Excel 复制出来并将其粘贴到另一个 IDE 来使文本看起来像上面那样。在 Excel 中显示为:
在工作簿中
在编辑区
立即window
基于 this site 看起来它在显示 '
字符时出现问题,但是如果它甚至无法正确读取它,我该如何将其显示为 fix/filter在 VBA 本身?
Option Explicit
dim mystring as String
dim regex as new RegExp
Private Function rgclean(ByVal mystring As String) As String
'function that find and replace string if contains regex pattern
'returns str
With regex
.Global = True
.Pattern = "[^ \w]" 'regex pattern will ignore spaces, word and number characters...
End With
rgclean = regex.Replace(mystring, "") '.. and replaces everything else with ""
End Function
尝试使用正则表达式。
确保您启用了正则表达式: 工具 > 参考 > 复选框:“Microsoft VBScript 正则表达式 5.5”
将“mystring”字符串变量传递给函数 (rgclean)。该函数将检查任何不是 space、单词 [A-Za-z] 或数字 [0-9] 的内容,将它们替换为“”,以及 returns 字符串。
该函数几乎可以删除字符串中的所有符号。不排除任何数字、Space 或单词。
这里是相反的做法。删除不包含在这组 62 中的所有字符:
ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
代码:
Const ValidCharacters As String = "ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String, L As Long, i As Long
Dim char As Variant
newString = myString
L = Len(newString)
For i = 1 To L
char = Mid(newString, i, 1)
If InStr(ValidCharacters, char) = 0 Then
newString = Replace(newString, char, "@")
End If
Next i
ReplaceSpecialCharacters = Replace(newString, "@", "")
End Function
注:
如果要保留字符,也可以向字符串 ValidCharacters
添加字符。