Solved

vba code to srt dynamic range in excel

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

733 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