我有在细胞以下字符串:删除 u00A0字符
我要拆分的字符串转换成仅包含文本词(如“CRMNegocios”)的阵列没有任何子弹,新的生产线,等等
要做到这一点,我已经写了下面的代码:
Sub Button1_Click()
Dim stringsToCheck As Variant
Dim element As Variant
Dim stripped As String
'Split cell value per vbLf
stringsToCheck = Split(Cells(42, 10).Value, vbLf)
MsgBox ("Total length of stringsToCheck is " & CStr(UBound(stringsToCheck)))
'Remove special characters - for testing only, it will set the cell with the last value of the array
For Each element In stringsToCheck
stripped = GetStrippedText(CStr(element))
Cells(42, 15) = stripped
Next element
End Sub
Private Function GetStrippedText(txt As String) As String
Dim regEx As Object
Set regEx = CreateObject("vbscript.regexp")
'\u0000-\u007F is for other special characters
regEx.Pattern = "[\u25A0\u00A0\u0000-\u007F]"
GetStrippedText = regEx.Replace(txt, "")
End Function
子弹被删除(这是\u25A0
)的前pected,但我仍然留下的文字字前\u00A0
字符:
我检查和regex is matching,为什么它没有在VBA删除?
正如在评论中提到,在单元格原文:
文本测试细胞,之后■ CRMNegocios
■ GestiondeProyectos
■ Emblue
■ Videoconferencia
代码运行:
Videoconferencia
对不起,你是否试图从字符串的开头删除这些非字字符?我想你可能只是使用'regEx.Pattern =“^ \ W +”'或者用你的方法'regEx.Pattern =“^ [\ u25A0 \ u00A0 \ s] +”'。请注意,'\ u0000- \ u007F'定义了一个ASCII表格范围,如果用它来替换,它将从字符串 –
中删除所有ASCII字母,数字,所有ASCII符号。谢谢@WiktorStribiżew,这确实有效。你能解释一下[在答案中,所以我可以接受:)]为什么我的初始方法在VBA中不匹配?并设置'regEx.Global = True'导致一切都被替换。虽然它在[here](https:// regex101)中似乎是正确的。com/r/KP1tF6/1) - 它应该只是找到特殊字符并删除它们,为什么我必须指定字符串开头的'^',如果我想要删除任何地方的特殊字符,会发生什么?字符串? –
我在下面发布了一个答案。 –