Making a VBA loop ignore hidden sheets

BENC93

New Member
Joined
Sep 29, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All

I've got myself a piece of code that splits each sheet in a workbook into it's own workbook. I've been using this forever (6+ years) in several different jobs I've had and it's done me well.
However, is there a means to make it skip hidden sheets as for the particular workbook I'm working off, theres a 60mb sheet that I don't need splitting (compared to the others which are only 60-70kb) and it slows down the process so much.

At present the code is as follows


VBA Code:
Sub SaveSheets()

' Save sheets as seperate workbooks
'
' Keyboard Shortcut: Ctrl+Shift+W


Dim wbDest As Workbook
Dim wbSource As Workbook
Dim sht As Object 
Dim strSavePath As String

On Error GoTo ErrorHandler
Application.ScreenUpdating = False 
strSavePath = "C:\Users\XXXXXX\OneDrive - XXXXXXX\Documents\Temp\New folder\" 'Change this to suit your needs
Set wbSource = ActiveWorkbook

For Each sht In wbSource.Sheets
sht.Copy

    Cells.Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

Set wbDest = ActiveWorkbook
wbDest.SaveAs strSavePath & "Dashboard - " & sht.Name
wbDest.Close 
Next

Application.ScreenUpdating = True

Exit Sub

ErrorHandler: 'Just in case something hideous happens
MsgBox "An error has occurred. Error number=" & Err.Number & ". Error description=" & Err.Description & "."
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi, you could test the visibility of the sheet at the start of your for loop, for example.

Rich (BB code):
For Each sht In wbSource.Sheets
    If sht.Visible = xlSheetVisible Then
        
        'the rest of your code within the for loop....
        
    End If
Next
 
Upvote 0
Solution
Hi, you could test the visibility of the sheet at the start of your for loop, for example.

Rich (BB code):
For Each sht In wbSource.Sheets
    If sht.Visible = xlSheetVisible Then
       
        'the rest of your code within the for loop....
       
    End If
Next
Worked a dream, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top