Solved

Make macro loop through multiple sheets?

Posted on 2014-02-12
10
3,513 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now