Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


VB Macro to Sort Columns in multiple workbooks

Posted on 2013-06-10
Medium Priority
Last Modified: 2013-06-11
I have an excel workbook with about 48 sheets. About 40 of those sheets are formatted the same way, and I need to sort from C2 to the end, E2 to the end, and G2 to the end. I have tried recording a macro, but it only works for one sheet, and it's hard to adapt because each sheet has a range of different length. After the sort, I then need to hide columns A,B,H,L, and M. Thanks for all the help!
Question by:indigo6
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
  • 3
  • 3
LVL 14

Expert Comment

ID: 39236537
Are the columns in any one sheet all of the same length? If not, can you tell which is the longest column?
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39236552
A sample file would be helpful

Author Comment

ID: 39238582
The column lengths always vary. The longest column is 1319 rows long.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 93

Expert Comment

by:Patrick Matthews
ID: 39238731
The column lengths always vary.

That's not a worry at all.  However...

I have an excel workbook with about 48 sheets. About 40 of those sheets are formatted the same way, ...

Which have to be sorted?  All worksheets?  Only the ~40 or so?  If all, then what sorting instructions should be used for the ~8 or so that are different?

...and I need to sort from C2 to the end, E2 to the end, and G2 to the end.

What columns on the worksheets are actually populated?  Are there header rows, or not?

This is why I asked for a sample file :)

Author Comment

ID: 39238752
Let me see if I can make a sample file. It's private data, so I'm a bit leery about it.

Also, I can move the sheets that don't need to be sorted to another workbook, so let's go on the assumption that all sheets need to be sorted.

Yes, there are header rows. Columns A-O have data.

LVL 93

Accepted Solution

Patrick Matthews earned 2000 total points
ID: 39238856
OK, assuming that Row 1 is where your headings are...

Sub SortThemAll()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        With ws
            .Columns.Hidden = False
            .Rows.Hidden = False
            .[a1].Sort Key1:=.[c1], Key2:=.[e1], Key3:=.[g1], Order1:=xlAscending, _
                Order2:=xlAscending, Order3:=xlAscending, Header:=xlYes
            .Columns("A:B").Hidden = True
            .Columns("H:H").Hidden = True
            .Columns("L:M").Hidden = True
        End With
    MsgBox "Done"
End Sub

Open in new window


Author Closing Comment

ID: 39239706
This is perfect! Nice separation of statements to make it easy to edit for future use too!

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

661 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