?
Solved

Make macro loop through multiple sheets?

Posted on 2014-02-12
10
Medium Priority
?
3,980 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 400 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
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.

 
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 1600 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

Industry Leaders: 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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

765 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