Solved

Make macro loop through multiple sheets?

Posted on 2014-02-12
10
3,861 Views
Last Modified: 2014-02-12
I have the following macro that deletes any hidden rows and columns from a worksheet.  It works fantastic; however, I have to execute it individually for each sheet in a multi-sheet workbook.

Is there a line(s) that I could add to this macro to force it to execute on all sheets in a workbook automatically?

Thanks!
Sub deletehidden()
For lp = 256 To 1 Step -1 'loop through all columns
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1 'loop through all rows
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next
End Sub

Open in new window

0
Comment
Question by:btkrausen
[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
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39854000
You can add a loop to iterate through all Sheets:

Sub LoopThroughSheets()
Dim Ws As Worksheet
    For Each Ws In ActiveWorkbook.Worksheets
        'your code here
    Next Ws
End Sub

Open in new window

0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 100 total points
ID: 39854034
Try this.
Try this.
[code]
Sub DeleteHiddenOnAllWS()
Dim ws As Worksheet
Dim lp As Long

    For Each ws In ThisWorkbook.Sheets

        With ws
            For lp = .Columns.Count To 1 Step -1    'loop through all columns
                If .Columns(lp).EntireColumn.Hidden = True Then .Columns(lp).EntireColumn.Delete
            Next lp
            For lp = .Rows.Count To 1 Step -1    'loop through all rows
                If .Rows(lp).EntireRow.Hidden = True Then .Rows(lp).EntireRow.Delete
            Next lp
        End With

    Next ws

End Sub

Open in new window

0
 

Author Comment

by:btkrausen
ID: 39854038
I have combined them as follows, but the macro doesn't appear to be working.  What have I done wrong?
Sub RemoveHidden()
Dim Ws As Worksheet
    For Each Ws In ActiveWorkbook.Worksheets
        For lp = 256 To 1 Step -1 'loop through all columns
        If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
        Next
        For lp = 65536 To 1 Step -1 'loop through all rows
        If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
        Next
    Next Ws
End Sub
                                            

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 18

Expert Comment

by:Steven Harris
ID: 39854060
"ActiveWorkbook.Worksheets" should work fine in Excel 2007 through Excel 2013; however, try using the "ThisWorkbook.Sheets" method as imnorie listed or even the "ActiveWorkbook.Sheets" method.
0
 

Author Comment

by:btkrausen
ID: 39854068
@ imnorie

I just tried your code, and it does the same as my modified one (above).  It looks like it's executing, and it doesn't give any errors, but when I check my other sheets the hidden columns are still hidden.

Any ideas?

Sample workbook included.  Hidden column is  F in the 20 oz CSD sheet.
Sample-Import-File.xls
0
 
LVL 81

Accepted Solution

by:
byundt earned 400 total points
ID: 39854241
btkrausen,
Your original macro was designed for Excel 2003. I have both fixed the error that prevented it from working on other worksheets as well as extended it to handle all the rows and columns in an Excel 2007 (or later) workbook. To improve speed, I restricted the macro to work on the used range, so rows or columns that lie beyond your data won't be tested. Finally, I turned screen updating off, so the macro will run faster in Excel 2003 and the screen won't flicker.

Brad

Sub RemoveHidden()
Dim Ws As Worksheet
Dim lp As Long
Dim rg As Range
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
    Set rg = Ws.UsedRange
    For lp = rg.Columns.Count To 1 Step -1 'loop through all columns
        If rg.Columns(lp).EntireColumn.Hidden = True Then rg.Columns(lp).EntireColumn.Delete
    Next
    For lp = rg.Rows.Count To 1 Step -1 'loop through all rows
        If rg.Rows(lp).EntireRow.Hidden = True Then rg.Rows(lp).EntireRow.Delete
    Next
Next Ws
End Sub

Open in new window

0
 
LVL 34

Expert Comment

by:Norie
ID: 39854252
The code I posted works on the uploaded workbook.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39854282
I agree with imnorie. His posted code works, though it does take a few seconds to run.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39854309
On a .xlsm workbook (with over a million rows), my code worked in the blink of an eye. imnorie's code took 38 seconds, or about 13 seconds per worksheet. Even in a .xls workbook (with only 65536 rows), imnorie's code is still taking about 1 second per worksheet.

You will need to decide whether deleting hidden rows or columns that lie beyond your data is worth waiting for.
0
 

Author Closing Comment

by:btkrausen
ID: 39854334
@byundt
Your code worked flawlessly and fast!  I'm not sure why I had issues with imnorie's.

Thanks to both of you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

691 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