|
|
迷茫的马克杯 · 从VBA中的范围中删除特殊字符开发者社区· 1 月前 · |
|
|
聪明伶俐的勺子 · 上海药物所提出机器学习辅助定向进化新方法· 2 年前 · |
|
|
光明磊落的铁链 · Java从json串中获取某个值 - ...· 2 年前 · |
|
|
踢足球的茶壶 · TypeScript笔记——枚举 - 掘金· 3 年前 · |
|
|
大气的手电筒 · ASP.NET Core Web API ...· 3 年前 · |
我已经创建了一个VBA代码来删除列中所有可用的特殊字符。例如,我有一个字母数字字符,列的每个单元格中都有一些特殊字符:假设在一个单元格中,我有一个值: abc@ 123 !-245,在执行我的代码后,我得到了输出abc 123 245,在这里,我的代码工作良好,可以删除所有特殊字符。我的代码如下:
Sub ReplaceSpecial()
Dim cel As Range
Dim strVal As String
Dim i As Long
Application.ScreenUpdating = False
For Each cel In Selection
strVal = cel.Value
For i = 1 To Len(strVal)
Select Case Asc(Mid(strVal, i, 1))
Case 32, 48 To 57, 65 To 90, 97 To 122
' Leave ordinary characters alone
Case Else
Mid(strVal, i, 1) = " "
End Select
Next i
cel.Value = strVal
Next cel
Application.ScreenUpdating = True
End Sub
现在,如果我想删除输出的空间,使输出看起来像abc123245,那么如何在VBA中做到这一点呢?输入: abc@ 123 !-245当前输出: abc 123 245必需输出: abc123245
发布于 2021-08-30 14:55:32
您可以用允许的字符构造一个新字符串。
Sub ReplaceSpecial()
Dim cel As Range
Dim strVal As String, temp As String
Dim i As Long
Application.ScreenUpdating = False
For Each cel In Selection
strVal = cel.Value
temp = vbNullString
For i = 1 To Len(strVal)
Select Case Asc(Mid(strVal, i, 1))
Case 32, 48 To 57, 65 To 90, 97 To 122
temp = temp & Mid(strVal, i, 1)
End Select
Next i
cel.Value = temp
Next cel
Application.ScreenUpdating = True
End Sub
发布于 2021-08-31 19:21:30
我对这封迟来的邮件的唯一意图是
Application.Match()
函数的一些特性(将字符串输入与有效字符进行比较)
String2Arr()
)。
不过,我不打算在这里展示更好或更快的代码。
Application.Match()
不仅允许在数组中执行1个字符搜索,而且允许在一次执行中比较甚至两个数组,即字符数组(基于原子化的字符串输入)与有效字符数组(空白、从A到Z的所有数字和字符)进行比较。由于
Application.Match
不区分大小写,因此只需使用小写字符即可。
所有输入字符的发现都返回它们在有效字符数组中的位置(否则会导致错误2042)。此外,有必要将"*“和"?”这两种外牌排除在外,否则将被视为调查结果。
Function ValidChars(ByVal s, Optional JoinResult As Boolean = True)
'Purp: return only valid characters if space,digits,"A-Z" or "a-z"
'compare all string characters against valid characters
Dim tmp: tmp = foundCharAt(s) ' get array with found positions in chars
'overwrite tmp array
Dim i As Long, ii As Long
For i = 1 To UBound(tmp)
If IsNumeric(tmp(i)) Then ' found in valid positions
If Not Mid(s, i, 1) Like "[?*]" Then ' exclude wild cards
ii = ii + 1
tmp(ii) = Mid(s, i, 1) ' get char from original string
End If
End If
ReDim Preserve tmp(1 To ii) ' reduce to new size
'join tmp elements to resulting string (if argument JoinResult = True)
ValidChars = IIf(JoinResult, Join(tmp, ""), tmp)
End Function
foundCharAt()
帮助函数
返回有效字符数组中找到的字符位置数组:
Function foundCharAt(ByVal s As String) As Variant
'Purp: return array of found character positions in chars string
'Note: (non-findings show Error 2042; can be identified by IsError + Not IsNumeric)
Dim chars: chars = String2Arr(" 0123456789abcdefghijklmnopqrstuvwxyz")
foundCharAt = Application.Match(String2Arr(s), chars, 0)
End Function
String2Arr()
帮助函数
在雾化字符串输入后分配单个字符数组:
Function String2Arr(ByVal s As String) As Variant
'Purp: return array of all single characters in a string
'Idea: https://stackoverflow.com/questions/13195583/split-string-into-array-of-characters
s = StrConv(s, vbUnicode)
String2Arr = Split(s, vbNullChar, Len(s) \ 2)
End Function
发布于 2021-08-30 15:45:09
使用正则表达式的对象,并使用否定式字符类替换所有不需要的字符。为示范目的:
Sub Test()
Dim str As String: str = "abc@123!-245"
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "[^0-9A-Za-z ]"
str = .Replace(str, "")
End With
Debug.Print str
End Sub
模式
[^0-9A-Za-z ]
是一个被否定的字符类,它捕获了所有的内容,即
,而不是
,字母、数字或空格字符。您将在这个在线
演示
中找到一个更深入的解释。
在写这篇文章的时候,我不确定你是否想省略空格字符。如果是这样,只需从模式中删除空间即可。
我想我应该使用
Like()
操作符放弃另一种选择:
For i = Len(str) To 1 Step -1
If Mid(str, i, 1) Like "[!0-9A-Za-z ]" Then
str= Application.Replace(str, i, 1, "")
|
|
迷茫的马克杯 · 从VBA中的范围中删除特殊字符开发者社区 1 月前 |
|
|
聪明伶俐的勺子 · 上海药物所提出机器学习辅助定向进化新方法 2 年前 |
|
|
踢足球的茶壶 · TypeScript笔记——枚举 - 掘金 3 年前 |