|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
VBA实现的方法:
效果
代码
Sub 选中内容字符去重()
Dim arr, str, r
arr = 每个字符保持进数组(Selection.Range.text)
arr = 数组去重(arr)
For Each r In arr
str = str & r
Next
Selection.Range.text = str
End Sub
Function 每个字符保持进数组(str As String)
Dim inputString As String
Dim characters() As String
Dim i As Integer
inputString = str
ReDim characters(1 To Len(inputString))
For i = 1 To Len(inputString)
characters(i) = mid(inputString, i, 1)
Next i
每个字符保持进数组 = characters
End Function
Function 数组去重(arr)
Dim i&, s, keys
Dim dic
Set dic = CreateObject("scripting.dictionary")
For i = 1 To UBound(arr) '去重
If arr(i) <> "" Then
If Not dic.Exists(arr(i)) Then dic.Add arr(i), Nothing
End If
Next
ReDim arr(0 To dic.count - 1)
keys = dic.keys
For i = 0 To dic.count - 1
arr(i) = keys(i)
Next
数组去重 = arr
End Function
|
|