Solved

Make macro loop through multiple sheets?

Posted on 2014-02-12
10
3,473 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 80

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 80

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 80

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

743 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

13 Experts available now in Live!

Get 1:1 Help Now