Sort Data in Ascending Order Based on Column Header Name

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
111
Office Version
  1. 365
Platform
  1. Windows
Hi,

Need help with some VB code please. I would like to sort data in ascending order based on column headers, the number of columns and rows may differ. So, sort ascending based on column header "Date".

Thanks in advance!
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Does your data always start up in cell A1?
 
Upvote 0
Yes it does! But the number of columns and rows might change.
 
Upvote 0
Try this:
Code:
Sub DynamicSort()

    Dim sortAdd As String
    Dim sortRange As Range

'   Find which column "Date" appears in
    On Error GoTo err_chk
    Rows("1:1").Find(What:="Date", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    On Error GoTo 0
    sortAdd = ActiveCell.Address(0, 0)
    
'   Set sort range by using current region
    Range("A1").CurrentRegion.Sort _
        key1:=Range(sortAdd), order1:=xlAscending, Header:=xlYes
        
    Exit Sub
    
'   Error handling
err_chk:
    If Err.Number = 91 Then
        MsgBox "No header row with title of Date", vbOKOnly, "ERROR!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
    
End Sub
 
Upvote 0
You are welcome!
 
Upvote 0
Hi, I spoke too soon. Most of the column is sorted, but not all. Any tips?
 
Upvote 0
I think it has something to do with the date format. I need to add code for "sort values that look like a date as date"
 
Upvote 0
Hi, I spoke too soon. Most of the column is sorted, but not all. Any tips?
It works on the Contiguous Range.
That means that it will go as far out as the first totally blank column, and down as far as the first totally blank row.
If you have some columns that are not being sorted, be sure to check to see if you have any blank columns between them and the rest of the data.
If you do not have this going on, do these columns that aren't sorting write contain formulas?
 
Upvote 0
No formulas, but the sort is sorting 8/1, then 8/10, then 8/11, then 8/2.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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