简体   繁体   中英

Sorting data using VBA and data in cell

I'm quite new to VBA and macros, and was looking for a bit of help sorting data using a macro. I've recorded doing what I was, and it has produced this: Macro1 Macro

Range("A19:L28").Select
ActiveWorkbook.Worksheets("EuropeanStocks").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("EuropeanStocks").Sort.SortFields.Add Key:=Range( _
    "h20:h28"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal

With ActiveWorkbook.Worksheets("EuropeanStocks").Sort
    .SetRange Range("A19:L28")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

My question is how do I make is so that the sort range is defined by the data in a cell. For this example with the sort range of h20:h28, the 20 and 28 parts would be constant, and I'd have a cell in the worksheet

Eg A1, which contains the letter of the column that needs to be sorted, Eg "i","j", etc..

How would I make it so A1 was read into the sort range?

The most versatile way would be to define a named range for the range you want to sort (or the range containing the range you want to sort), but for this simple requirement, you can simply wrap a Range call to the cell containing the sort range (eg A1) in another Range call, thus obtaining the desired range. Eg:

.SetRange Range(Range("A1"))

EDIT: This assumes that A1 contains a full range reference (eg it contains "A19:L28"). If you want to construct the target range from other cells partially, one solution could be to construct the range reference, for example:

.SetRange Range(Range("A1") & "19" & ":" & Range("A2") & "28")

See the code below. You need to place the value in Range("A1") into a variable and then set your range in the code with that variable.

Dim ws As Worksheet, c As String, rSort As Range, rData As Range
ws = Sheets("EuropeanStock")

With ws

    c = .Range("A1").Value

    Set rData = .Range("A19:L28")
    Set rSort = .Range(c & "20:" & c & "28")

    With rData.Sort

        With .SortFields
            .Clear
            .Add Key:=rSort, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        End With

        .SetRange rData
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

    End With

End With

Just place the columns that you want to sort in A1, and separate them with a slash / (ie a/f/h/s )

This will sort all of them one by one :

Dim NewRange As String, _
    DataRange As String, _
    Sp() As String

Sp = Split(Range("A1"), "/")

With ActiveWorkbook.Worksheets("EuropeanStocks")
    For i = LBound(Sp) To UBound(Sp)
        NewRange = Sp(i) & "19:" & Sp(i) & "28"
        DataRange = Sp(i) & "20:" & Sp(i) & "28"
        With .Sort
            .SortFields.Clear
            .SortFields.Add _
                Key:=Range(DataRange), _
                SortOn:=xlSortOnValues, _
                Order:=xlDescending, _
                DataOption:=xlSortNormal

            .SetRange Range(NewRange)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next i
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM