Terminal Digit Sorting Macro? Also adding to existing macro? XL-07

Dark91zc

Board Regular
Joined
Aug 1, 2013
Messages
62
Hello All,

And thank you for looking and helping. This is what i am doing. My current code allows me to import folder names into the excel spreadsheet. When the data is imported it is broken down into 4 columns. They are the following Last Name, First Name, MDR, and D.O.B. Everything is correct except the sorting of the MDR. It is a Terminal Digit Filing system used by the customer. So here is an example. You have an 8 digit code or number, 00234589 this would be the patients MDR. The correct way to "read" it is as follows, last 2 digits first, 89, middle 2 digits second, 45, then the first 4 digits, 0023. Now here is the trick, it stays the same visually as it is imported but sorted this way 89,45,0023.

Is there a way that this can be in a macro or script? If so can this be done during the import of the folder directory to excel, or will it have to be a stand alone run after macro / script?

This is what my sheet looks like
Last Name:
First Name:MDR:D.O.B.:
row
1
00778742
row
2
01094917
row
3
00244412
row
4
00836117
row
5
00909208
row
6
00939129
row
7
00898637
row
8
00914241
row
9
00953518

<tbody>
</tbody>

What I would like it to look like is this.
Last Name:
First Name:MDR:D.O.B.:
new row1
old row5
00909208
new row2
old row3
00244412
new row3
old row2
01094917
new row4
old row4
00836117
new row5
old row9
00953518
new row6
old row6
00939129
new row7
old row7
00898637
new row8
old row8
00914241
new row9
old row1
00778742

<tbody>
</tbody>

I am hoping that if and when this is sorted the other columns A,B, and D follow C still.

The following is the code / script i would like the code to be put into so it can run with the main macro.
Code:
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
  
Public Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type
Sub ImportFilesInFolder()
    'Workbooks.Add ' create a new workbook for the file list
    ' add headers
    'With Range("A1")
    '    .Formula = "Folder contents:"
    '    .Font.Bold = False
    '    .Font.Size = 12
    'End With
    Range("A1").Formula = "Last Name:"
    Range("B1").Formula = "First Name:"
    Range("C1").Formula = "MDR:"
    Range("D1").Formula = "D.O.B.:"
'    Range("E1").Formula = "Date Last Accessed:"
'    Range("F1").Formula = "Date Last Modified:"
'    Range("G1").Formula = "Attributes:"
'    Range("H1").Formula = "Short File Name:"
    Range("A1:H1").Font.Bold = True
    Msg = "Select a location containing the folders you want to list."
    'Directory = GetDirectory(Msg)
    'list all files included subfolders
    ListFilesInFolder GetDirectory(Msg), True

End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the files in SourceFolder
' example: ListFilesInFolder "C:\FolderName\", True
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
Dim Elements As Variant
Dim Temp As String

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    r = Range("A65536").End(xlUp).Row + 1
    For Each SubFolder In SourceFolder.SubFolders
        ' display file properties
        'Temp = StrReverse(SubFolder.Name)
        'Temp = Right(Temp, Len(Temp) - InStr(Temp, "."))
        'Temp = StrReverse(Temp)
        
        If InStr(SubFolder.Name, "_") Then
             Elements = Split(Replace(SubFolder.Name, "^", "_"), "_")
            If UBound(Elements) = 3 Then
                Cells(r, 1) = Elements(0)
                Cells(r, 2) = Elements(1)
                Cells(r, 3) = Elements(2)
                Cells(r, 4) = Elements(3)
               
    
    '        Cells(r, 2).Formula = FileItem.Size
    '        Cells(r, 3).Formula = FileItem.Type
    '        Cells(r, 4).Formula = FileItem.DateCreated
    '        Cells(r, 5).Formula = FileItem.DateLastAccessed
    '        Cells(r, 6).Formula = FileItem.DateLastModified
    '        Cells(r, 7).Formula = FileItem.Attributes
    '        Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
            
                r = r + 1 ' next row number
          End If
      End If
    Next SubFolder
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.path, True
        Next SubFolder
    End If
    Columns("A:D").AutoFit
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
End Sub


Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO
    Dim path As String
    Dim r As Long, x As Long, pos As Integer

    bInfo.pidlRoot = 0&
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
  End If
    bInfo.ulFlags = &H1
    x = SHBrowseForFolder(bInfo)
    If x = 0 Then End
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
  End If
End Function


Once again thank you.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This put a formula in an empty column E to parse the MDR code (12345678) into a number =78561234. It then sorts the rows on column E and deletes it after the sort.

Code:
[COLOR=darkblue]Sub[/COLOR] ImportFilesInFolder()
    [COLOR=green]'Workbooks.Add ' create a new workbook for the file list[/COLOR]
    [COLOR=green]' add headers[/COLOR]
    [COLOR=green]'With Range("A1")[/COLOR]
    [COLOR=green]'    .Formula = "Folder contents:"[/COLOR]
    [COLOR=green]'    .Font.Bold = False[/COLOR]
    [COLOR=green]'    .Font.Size = 12[/COLOR]
    [COLOR=green]'End With[/COLOR]
    Range("A1").Formula = "Last Name:"
    Range("B1").Formula = "First Name:"
    Range("C1").Formula = "MDR:"
    Range("D1").Formula = "D.O.B.:"
[COLOR=green]'    Range("E1").Formula = "Date Last Accessed:"[/COLOR]
[COLOR=green]'    Range("F1").Formula = "Date Last Modified:"[/COLOR]
[COLOR=green]'    Range("G1").Formula = "Attributes:"[/COLOR]
[COLOR=green]'    Range("H1").Formula = "Short File Name:"[/COLOR]
    Range("A1:H1").Font.Bold = [COLOR=darkblue]True[/COLOR]
    Msg = "Select a location containing the folders you want to list."
    [COLOR=green]'Directory = GetDirectory(Msg)[/COLOR]
    [COLOR=green]'list all files included subfolders[/COLOR]
    ListFilesInFolder GetDirectory(Msg), [COLOR=darkblue]True[/COLOR]
    
    [B][COLOR=darkblue]With[/COLOR] Range("E2:E" & Range("C" & Rows.Count).End(xlUp).Row)
        .Formula = "=(Right(C2,2)&Mid(C2,5,2)&Text(Left(C2,4),""0000"")) * 1"
        .EntireRow.Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        .ClearContents
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR][/B]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Thanks everything is looking good so far but it does not look like it is sorting the middle 2 numbers correct. what it looks like it is doing is sorting the last 2 in order then the first 4.
My list came out like this.
Last Name:First Name:MDR:
D.O.B.:
00713901
00886602
01024202
00960803
00916906
01116107
01033807
00909208
00799209
01058111
00175312
00244412
00711213
00712914
00239715

<tbody>
</tbody>

each group of numbers has to be in order but i would not know how to sort it i have looked at the code and can see how it works but get confused.
 
Upvote 0
This is the result I got using your recent MDR data. It works correct for me.

ABCD
1Last Name:First Name:MDR:D.O.B.:
2
00713901​
3
01024202​
4
00886602​
5
00960803​
6
00916906​
7
01033807​
8
01116107​
9
00909208​
10
00799209​
11
01058111​
12
00244412​
13
00175312​
14
00711213​
15
00712914​
16
00239715​

<tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
I just tried it on a new spread sheet and the same thing. I am going to try a different computer.
 
Upvote 0
This is what my sort is looking like
1017139
2028866
2201024
3039608
6069169
7101116
7801033
8089092
9097992
11111058
12121753
12122444
13137112
14147129
15152397
15157169
16162766
17178361
17911094

<colgroup><col style="width:48pt" width="64"> </colgroup><tbody>
</tbody>

I am kinda confused it looks like it is going last 2 first 4 then middle 2. Maybe i am looking to far into it.
 
Upvote 0

Forum statistics

Threads
1,215,488
Messages
6,125,092
Members
449,206
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top