将数字分为唯一的排序数字,显示在用户表单的标签中 [英] Divide numbers into unique sorted digits displayed in a label on a userform
本文介绍了将数字分为唯一的排序数字,显示在用户表单的标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数字分成唯一的排序数字。
例如,数字可以是127425,而我想要的结果是12457,表示已排序并删除了重复项。我认为最好是用示例来解释:
I want to divide numbers into unique sorted digits. For example, the number can be 127425 and I would like 12457 as the result, meaning sorted and duplicate removed. I think the best is to explain with example:
+---------+--------+
| Number | Result |
+---------+--------+
| 127425 | 12457 |
+---------+--------+
| 2784425 | 24578 |
+---------+--------+
| 121 | 12 |
+---------+--------+
| 22222 | 2 |
+---------+--------+
| 9271 | 1279 |
+---------+--------+
最长结果只能是123456789。
The longest result can be only 123456789.
我认为我们不需要为此使用数组(没有定界符),但是使用子字符串可能可以完成这项工作。我只是不知道从哪里开始,因此没有代码。
I don't think we need an array for that (no delimiter), but the use of substring could probably do the job. I just don't know where to begin, hence no code.
欢迎任何想法。谢谢。
推荐答案
请尝试下一个功能,
Function RemoveDuplSort(x As String) As String
Dim i As Long, j As Long, arr As Variant, temp As String
'Dim dict As New Scripting.Dictionary 'in case of reference to 'Microsoft Scripting Runtime,
'un-comment this line and comment the next one:
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To Len(x)
dict(Mid(x, i, 1)) = 1
Next i
arr = dict.Keys
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) > arr(j) Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next j
Next i
RemoveDuplSort = Join(arr, "")
End Function
可以通过以下方式调用:
It can be called in this way:
Sub testRemoveDuplSort()
Dim x As String
x = "2784425" 'x = myLabel.Caption
Debug.Print RemoveDuplSort(x)
End Sub
这篇关于将数字分为唯一的排序数字,显示在用户表单的标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文