Solved

Make macro loop through multiple sheets?

Posted on 2014-02-12
10
3,664 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
  • 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 33

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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 33

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

821 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