• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to define Column to sort with VBA

ThrottleWorks

Excel Ninja
Hi,

I am trying to sort a data.

I am finding header "reference" in the sheet and sort the data with base as "reference".

My problem is once I am finding 'reference' in the data, macro takes Column "U" as static.
The header 'reference' in U Column but my problem is 'reference' can be in Column T or R also.

Howe do I make my sorting Column dynamic.

I want to change ":=Range("U2:U65000")," to a dynamic range.
Can anyone please help me this.

Kindly note I have recorded below mentioned macro for my use.

Code:
Collate.Select
    Range("a1").Select
    Selection.AutoFilter
    Cells.Find(What:="Reference", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, Search Direction:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    ActiveWorkbook.Worksheets("CollatedData").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("CollatedData").AutoFilter.Sort.SortFields.Add Key _
        :=Range("U2:U65000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("CollatedData").AutoFilter.Sort
        .Header = xlYes
        .Match Case = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Back
Top