Import only Visible Tabs within Workbook

Miche25

New Member
Joined
Jan 5, 2016
Messages
24
I have a folder named Import containing numerous excel files (all starting with OBR) and within those files I only want to import the visible tabs not any hidden tabs into an excel file called 'Ops Board Report'.
The Import folder where all the files I want to import into the Board Report are within
"C:\Users\MyName\Documents\Board Report\Board Report\Import"
the Board Report is within
"C:\Users\MyName\Documents\Board Report\Board Report\Ops Board Report.

How do I go about doing this?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Many thanks, for your help.

Sorry, I am new to VBA, In the code where would I put If ws.Visible = xlSheetVisible Then

Many thanks.
 
Upvote 0
Immediately inside the For Next loop, like this.
Code:
Public Sub Copy_Visible_Sheets()

    Dim sourceFiles As String
    Dim destinationWorkbook As String, destWb As Workbook
    Dim sourcePath As String, fileName As String
    Dim ws As Worksheet
    
    sourceFiles = "C:\Users\MyName\Documents\Board Report\Board Report\Import\OBR*.xl*"
    destinationWorkbook = "C:\Users\MyName\Documents\Board Report\Board Report\Ops Board Report\Ops Board Report.xlsx"  'If needed, change xlsx to correct extension
    
    Application.ScreenUpdating = False
    
    Set destWb = Workbooks.Open(destinationWorkbook)
    
    sourcePath = Left(sourceFiles, InStrRev(sourceFiles, "\"))
    
    fileName = Dir(sourceFiles)
    Do While fileName <> ""
        Workbooks.Open fileName:=sourcePath & fileName, ReadOnly:=True
        For Each ws In ActiveWorkbook.Sheets
            If ws.Visible = xlSheetVisible Then
                ws.Copy after:=destWb.Sheets(destWb.Sheets.Count)
                destWb.Sheets(destWb.Sheets.Count).Name = fileName & " " & ws.Name
            End If
        Next
        Workbooks(fileName).Close
        fileName = Dir()
    Loop
    
    destWb.Close True
    
    Application.ScreenUpdating = True
    
End Sub
Put the code in a module of a macro-enabled workbook. I've assumed that your report workbook is a .xlsx file - change that line in the code if it is different.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
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