Sort one column in a 2D array

mtenorio

New Member
Joined
Sep 5, 2014
Messages
18
Hi guys! I have several text files that I imported using excel VBA. I have extracted the needed values and stored them in a 2D array.

I'd like to sort a column of the 2D array. Do you have ideas how to go about this?

The first column corresponds to the quantity and the second column corresponds to the product name.

My goal is to get the top 10 products with the highest quantities. So I thought I could sort the quantities by ascending order and get the first 10 indexed array.

This is my code so far:

Code:
Dim l As Long, M As Long, srt_current() As String
Dim temp_current() As String

Application.ScreenUpdating = False
a = "C:\Users\MAGTenorio\Documents\Hopital\LMH_CDF"              'root location of the folders
b = Worksheets("Sheet1").Range("C6:C6").Value                           'gets the value of the supplier code
c = Worksheets("Sheet1").Range("C8:C8").Value                           'gets the value of the current period
d = Worksheets("Sheet1").Range("C10:C10").Value                         'gets the value of the previous period
period_m1 = (WorksheetFunction.VLookup(Left(c, 2), Range("P2:U13"), 3)) & Right(c, 2)
period_m2 = (WorksheetFunction.VLookup(Left(c, 2), Range("P2:U13"), 4)) & Right(c, 2)
period_m3 = (WorksheetFunction.VLookup(Left(c, 2), Range("P2:U13"), 5)) & Right(c, 2)
period_m12 = (WorksheetFunction.VLookup(Left(c, 2), Range("P2:U13"), 6)) & Range("O2")

Sheets("Sheet1").Select
ActiveSheet.Unprotect
'Code for the current month
If FileName(a, c, b) = "" Then
    Range("H7").Value = "NO DATA"
    MsgBox "There is no file found for the current month."
Else
    Open FileName(a, c, b) For Input As #1               'opens the sales file for the current month
    row_number = 0
    number_lines = 0
    Do Until EOF(1)                                  'reads the text line per line until the end of file
        Line Input #1, LineFromFile
        quantity_current = Mid(LineFromFile, 237, 10) 'gets the quantity
        product_current = Mid(LineFromFile, 104, 80)  'gets the product name
        quantity_current_all = quantity_current_all & quantity_current & ";"  'stores the values of the quantity
        product_current_all = product_current_all & product_current & ";"     'stores the values of the produc names
        number_lines = number_lines + 1
        row_number = row_number + 1
        'Debug.Print quantity_current_all
    Loop
    Close #1
        
    ReDim temp_current(1 To number_lines, 1 To 2) As String
    'Save values on 1 2D array
    For i = 1 To number_lines - 2
        temp_current(i, 2) = CStr(Split(product_current_all, ";")(i))
        If IsNumeric(Split(quantity_current_all, ";")(i)) Then
            temp_current(i, 1) = CLng(Split(quantity_current_all, ";")(i))
        Else
            temp_current(i, 1) = 0
        End If
        'Debug.Print CLng(Split(quantity_current_all, ";")(I)), CStr(Split(product_current_all, ";")(I))
        Debug.Print temp_current(i, 1), temp_current(i, 2)
    Next i

    
End If

Here is the output array:
Code:
0             DISCOTRINE5mg/24h Disp tr B/30                                                  
18            HEMIGOXINE NATIV0,125mg Cpr B/30                                                
12            PREVISCAN20mg Cpr B/30                                                          
8             DIGOXINE0,25mg-Cpr-B/30                                                         
9             PERMIXON160mg Gél B/60                                                          
7             SOLIAN200mg Cpr séc B/30                                                        
16            VOGALENE LYOC7,5mg Lyoph or B/16                                                
24            AMLOR=AMLOPIDINE10mg Gél B/30                                                   
10            BETADINE TULLEPans B/10/10x10                                                   
20            DISCOTRINE10mg/24h Disp tr B/30                                                 
7             CHIBRO-PROSCAR5mg Cpr pell B/28                                                 
5             KARDEGIC160mg Pdr or 30sach                                                     
1             ELISOR20mg Cpr séc B/28

Thanks a lot in advance!!!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,216,025
Messages
6,128,336
Members
449,443
Latest member
Chrissy_M

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