Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for better defined range on Excel 2010?

Tags:

excel

vba

Looking for help to make my macro more range-friendly. Excel is 2010 version.

ActiveWorkbook.Worksheets("AddressList").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("AddressList").Sort.SortFields.Add Key:=Range( _
        "G2:G1374"), SortOn:=xlSortOnValues, Order:=xlAscending,    
DataOption:= _
    xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("AddressList").Sort
    .SetRange Range("A1:J1374")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Question: How to change from predefined range (G2:G1374 and A1:J1374) to something based on columns and used cells?

like image 743
Aniskos Avatar asked Dec 12 '25 21:12

Aniskos


1 Answers

There are better ways to code a Range.Sort method. Using the Range.CurrentRegion property is ideal for referencing a block of data radiating out from A1.

with worksheets("AddressList")  
    with .cells(1, 1).currentregion
        .Cells.Sort Key1:=.Columns(7), Order1:=xlAscending, _
                    Orientation:=xlTopToBottom, Header:=xlYes
    end with    
end with

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!