Solved

vba code to srt dynamic range in excel

Posted on 2011-09-16
4
454 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now