Link to home
Start Free TrialLog in
Avatar of Ben Cheetham
Ben Cheetham

asked on

Ordering Digits using VBA

Hi EE

I’ve got four digits which I want to put in order so that I can compare them with other sets of four digits to see if they are unique.  For example, 2384 and 3842 once put in order both give 2348 and, under my rationale, they’d be considered not unique.

Any ideas?
Many thanks.
Ben
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Here it is as a function.

Private Function OrderNumber(strNum As String) As String
Dim intChar As Integer
Dim intNum As Integer
Dim varOrder As Variant

varOrder = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
For intNum = 0 To 9
    For intChar = 1 To Len(strNum)
        If Mid$(strNum, intChar, 1) = varOrder(intNum) Then
            OrderNumber = OrderNumber & Mid$(strNum, intChar, 1)
        End If
    Next
Next
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ben Cheetham
Ben Cheetham

ASKER

That’s so quick and a perfect solution.  Many thanks.
Ben
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2019
              Experts Exchange Top Expert VBA 2018, 2019
              Experts Exchange Distinguished Expert in Excel 2018