0

I need vba code to help me sort multiple tables in the same sheet without merging them into one table. I have a range of data, from B14:V18, another from B21:V39, and another from B42:V108. I need to sort the data based on the values in column L. However, it needs to be dynamic, because the number of rows in each table could change on any given day. Using the normal sort function in excel just merges all of the data into one table, not keeping them seperate.

The columns will always be B:V, but the rows will be changing. As well, the next table will always be located 3 rows below the last cell of the previous one.

4
  • 1
    @SkipIntro is right, but I couldn't resist. Commented May 30, 2013 at 14:03
  • 1
    @DougGlancy Give a man a fish, etc... :o) Commented May 30, 2013 at 14:13
  • 1
    @SkipIntro, it's true, but I wanted to know how to do it, so selfishly took the fish for my own. Commented May 30, 2013 at 14:17
  • I need to sort the info in column L from biggest to smallest for each table, by the way. Commented May 30, 2013 at 14:21

2 Answers 2

2

I realize this thread is dead but wanted to provide a likely solution to the OP's problem. I used Doug's macro and encountered the same problem the OP did. I simply added .SortFields.Clear before the .SortFields.Add line, which then caused the macro to run correctly. See my post here for my code using two sort criteria.

Sub SortTables()
Dim lo As Excel.ListObject
Dim ws As Excel.Worksheet

Set ws = ActiveSheet
For Each lo In ws.ListObjects
    With lo.Sort
        .SortFields.Clear
        .SortFields.Add Key:=lo.ListColumns("test2").Range, Order:=xlAscending
        .Header = xlYes
        .Apply
    End With
Next lo
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

This will sort all the tables on the active worksheet by a column named "test3". It should work no matter how the tables are situated on the sheet:

Sub SortTables()
Dim lo As Excel.ListObject
Dim ws As Excel.Worksheet

Set ws = ActiveSheet
For Each lo In ws.ListObjects
    With lo.Sort
        .SortFields.Add Key:=lo.ListColumns("test2").Range, Order:=xlAscending
        .Header = xlYes
        .Apply
    End With
Next lo
End Sub

Tested in Excel 2010

3 Comments

Doesn't seem to be working for some reason. Do I need to have a certain cell selected? I literally copied that code into excel and changed "test 2" to "Upside", which is the word in my L1. Would this make sense?
I think I may have misunderstood. Are these three actual Tables, or just areas on a sheet?
And is your version Excel 2007 or later? If so, it should work as you describe, sorting each table by the column with a header of "Upside."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.