Need help sorting values across columns

harrysolomon

New Member
Joined
Mar 6, 2002
Messages
25
I have an Excel worksheet which contains an ISO Country Code value in a cell and then the next cell to the right contains the Country name corresponding to that ISO Country code.

For example, cell A1 contains the value "US" and cell B1 contains the name "United States”, cell C1 contains "CH" and cell D1 contains "Switzerland", cell E1 contains “DE” and cell F1 contains “Germany” .

This country code & description information exists across a single row and the number of cells on a row which contain a value will vary.

The information listed across the row is currently sorted in alphabetical order on the ISO Country Code value and I want to re-sort this information alphabetically within the row by the ISO Code description using VBA.

So using the previous example data, if the original order of the data in the row is
cell A1 = "US" & cell B1 = "United States”
cell C1 = “CH” & cell D1 = “Switzerland”
cell E1 = "DE" & cell F1 = "Germany"

After sorting, the desired end result should be
cell A1 = "DE" & cell B1 = "Germany"
cell C1 = “CH” & cell D1 = “Switzerland”
cell E1 = "US" & cell F1 = "United States”


Thanks in advance for any help.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Untested - try on a copy of your sheet:
Code:
Sub SortByCodeName()
Dim lC As Long, R As Range, vA As Variant, i As Long, j As Long
lC = Cells(1, Columns.Count).End(xlToLeft).Column
Set R = Range("A1", Cells(1, lC))
vA = R.Value
For i = LBound(vA, 2) + 1 To UBound(vA, 2) - 2 Step 2
    For j = i + 2 To UBound(vA, 2) Step 2
        If vA(1, i) > vA(1, j) Then
            temp1 = vA(1, i)
            vA(1, i) = vA(1, j)
            vA(1, j) = temp1
            temp2 = vA(1, i - 1)
            vA(1, i - 1) = vA(1, j - 1)
            vA(1, j - 1) = temp2
        End If
    Next j
Next i
R.Value = vA
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,915
Members
449,132
Latest member
Rosie14

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