[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

vba code to srt dynamic range in excel

Posted on 2011-09-16
4
Medium Priority
?
500 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 2000 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 35

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

650 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