Copy hidden sheets from one workbook to another

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
OK, I have a workbook that is able to import worksheets from other workbooks, however, I would like to hide the information that is being imported so that users can't just double click on the workbook and edit it. I am having trouble getting the main workbook to import the data worksheets when I set them to very hidden and password protect the VBA. Here is what I started with and it worked great to import visible, unprotected worksheets:


Code:
Private Sub InstallProducts_Click()
Application.ScreenUpdating = False
If FilePath.Value = "" Then
MsgBox "Please Select a File to Install", vbOKOnly
Exit Sub

Else
Unload Me
unprotect
Dim wkbSource As Workbook
Dim wkbTarget As Workbook
Dim WorkbookName As String
WorkbookName = ThisWorkbook.Name

    Set wkbTarget = Workbooks(WorkbookName)
    Set wkbSource = Workbooks.Open(FilePath.Value)

    wkbSource.Sheets.Copy After:=wkbTarget.Sheets(wkbTarget.Sheets.Count)

wkbSource.Activate

ActiveWorkbook.Close SaveChanges:=True

End If
Application.ScreenUpdating = True
End Sub
This is what I have done to modify it:

Code:
Private Sub InstallProducts_Click()
Application.ScreenUpdating = False
If FilePath.Value = "" Then
MsgBox "Please Select a File to Install", vbOKOnly
Exit Sub

Else
Unload Me
unprotect
Dim wkbSource As Workbook
Dim wkbTarget As Workbook
Dim WorkbookName As String
WorkbookName = ThisWorkbook.Name

    Set wkbTarget = Workbooks(WorkbookName)
    Set wkbSource = Workbooks.Open(FilePath.Value)


For Each sh In wkbSource.Worksheets
    If sh.Name <> "Sheet1" Then
        wkbSource.Sheets.Copy After:=wkbTarget.Sheets(wkbTarget.Sheets.Count)
    End If
Next sh
wkbSource.Activate

ActiveWorkbook.Close SaveChanges:=False

End If
Application.ScreenUpdating = True
End Sub
For some reason my modified code is looping a bunch of times and copying each worksheet 12 times, not just once. I would like this code to open a workbook (selected by a user in a form, which already works great), unhide all sheets, copy the sheets, then close the imported workbook without saving so that the sheets are still hidden if the user opens the imported workbook in excel later. Any insight would be greatly appreciated! Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,224,609
Messages
6,179,881
Members
452,948
Latest member
Dupuhini

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