Solved

vba code to srt dynamic range in excel

Posted on 2011-09-16
4
467 Views
Last Modified: 2012-06-22
I have multiple sheets that need to be sorted based on the values in Column I.  I have used code given below but this is throwing error.

    lc = workbook1.ActiveSheet.Cells(Rows.Count, 8).End(xlUp).row
     Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("I6" & lc) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A5" & lc)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


Am I missing anything??
0
Comment
Question by:vb2178
4 Comments
 
LVL 6

Accepted Solution

by:
TinTombStone earned 500 total points
ID: 36548256
Try:
lc = workbook1.ActiveSheet.Cells(Rows.Count, 8).End(xlUp).Row
     Range("A5").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("I6") _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range(Selection.Address)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Open in new window

0
 
LVL 19

Expert Comment

by:akoster
ID: 36548266
what you are doing is determining the last used cell in column H and storing the row number in Ic (eg with value 8)
you then select some cells with which you do nothing
you remove previous sort rules
and add a sort rule based on single cell (with Ic value 8 : I68)
which you edit with range based on the single cell A58

you could use this :
Sub sort_on_i()
    UsedRange.Sort Range("I:I"), xlDescending   
End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
ID: 36549150
This will sort each worksheet in the workbook.

It can be altered so only certain worksheets will be sorted.

Option Explicit

Sub SortSheets()
Dim ws As Worksheet
Dim rngData As Range
Dim LastCol As Long
Dim LastRow As Long

    For Each ws In Worksheets
    
        LastCol = ws.Cells(5, 1).End(xlToRight).Column
        LastRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
        
        Set rngData = ws.Range("A5:A" & LastRow)
        
        Set rngData = rngData.Resize(, LastCol)
        
        ws.Sort.SortFields.Clear
        ws.Sort.SortFields.Add Key:=ws.Range("I6:I" & LastRow), SortOn:=xlSortOnValues, _
                                                                        Order:=xlDescending, DataOption:=xlSortNormal
        With ws.Sort
            .SetRange rngData
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
     Next

End Sub

Open in new window

0
 

Author Closing Comment

by:vb2178
ID: 36551755
Simple and perfect solution
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question