Solved

How do I get an Excel 2007 sort macro to recognize variable columns and rows?

Posted on 2014-10-17
4
188 Views
Last Modified: 2014-10-19
I recorded this macro.  It fails to recognize variability when adding/deleting rows and columns.  Although I have made this sheet into a table, it still does not recognize the variablity.  Any suggestions on building a sort macro to account for a sheet or table sort with row and column variablity is appreciated.  Attached is the spreadsheet.  The macro is "Test".

    ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
        SortFields.Add Key:=ActiveCell.Offset(0, -21).Range("A1:A16"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Test.xlsm
0
Comment
Question by:cowboywm
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Steve
ID: 40387537
Roughly...
lLastRow = ActiveWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    ActiveWorkbook.Worksheets("Sheet1").ListObjects("Sheet1").Sort. _
         SortFields.Clear
     ActiveWorkbook.Worksheets("Sheet1").ListObjects("Sheet1").Sort. _
         SortFields.Add Key:=ActiveCell.Offset(0, -21).Range("A1:A" & lLastRow), SortOn:= _
         xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
     With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Sheet1").Sort
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
     End With

Open in new window

0
 
LVL 7

Expert Comment

by:Steve
ID: 40387540
Renamed for your sheet...
lLastRow = ActiveWorkbook.Worksheets("Attendance").Cells(Rows.Count, 1).End(xlUp).Row
    ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
         SortFields.Clear
     ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
         SortFields.Add Key:=ActiveCell.Offset(0, -21).Range("A1:A" & lLastRow), SortOn:= _
         xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
     With ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
     End With

Open in new window

0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40387542
Since you've defined this as an Excel Table, you can continue using structured referencing.  Replace the second command above with the one here (in lines 4-6):
Sub Test()
    ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort. _
        SortFields.Add Key:=Range("Attendance[Role]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Attendance").ListObjects("Attendance").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Open in new window


This guarantees that the sort will be dynamic - that is, follow the current defined range of the table "Attendance".
-Glenn
0
 

Author Closing Comment

by:cowboywm
ID: 40391234
Glenn, your solution worked the best.  Thank you for your help.  I can now duplicate this functionality.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

785 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