Solved

vba code to srt dynamic range in excel

Posted on 2011-09-16
4
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Arno Koster
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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

719 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