Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90301

How to sort using column header as reference?

$
0
0

Is there a way in VBA to sort a column using it's header name? Since the column may change regularly, I cannot reference the column number to sort it, I need to use the column header for sorting. For Example, I have Order status (column header) in Column C and I need to sort this Order status instead of mentioning it as Column C.

This is the code I have so far:

'Sort the "Order Status(Column C)"'Clear out any previous Sorts that may be leftover
ws.Sort.SortFields.Clear

'range that includes all columns to sort
Set Rngsort = ws.UsedRange

'Columns with keys to sort
Set RngKey1 = ws.Range("C1")

'Perform the sort
With ws.Sort
    Rngsort.Sort Key1:=RngKey1, Order1:=xlAscending, header:=xlYes, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlSortColumns, DataOption1:=xlSortNormal
End With

Viewing all articles
Browse latest Browse all 90301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>