VBA to import sheets from a protected sheet and workbook, when copy paste is disabled

Godwin117

Board Regular
Joined
Dec 19, 2019
Messages
68
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have multiple excel documents that are protected, so the users can't alter any of the information. I am using the code below and keep getting an error, Method'Copy' of object'_Worksheet' failed. Wasn't sure why I was getting that error. I have done some research and can't seem to find the answer to the problem.

Dim fnameList, fnameCurFile As Variant
Dim countFiles, countSheets As Integer
Dim wksCurSheet As Worksheet
Dim wbkCurBook, wbkSrcBook As Workbook

fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="Choose Excel files to merge", MultiSelect:=True)

If (vbBoolean <> VarType(fnameList)) Then

If (UBound(fnameList) > 0) Then
countFiles = 0
countSheets = 0

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Set wbkCurBook = ActiveWorkbook

For Each fnameCurFile In fnameList
countFiles = countFiles + 1

Set wbkSrcBook = Workbooks.Open(Filename:=(fnameCurFile), UpdateLinks:=0)

For Each wksCurSheet In wbkSrcBook.Sheets
countSheets = countSheets + 1
wksCurSheet.Copy After:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
Next

wbkSrcBook.Close SaveChanges:=False
Sheets("STB").Select

Next

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Dim answer As Integer
answer = MsgBox("Import complete, would you like to roll up the units?", vbQuestion + vbYesNo + vbDefaultButton2, "Import complete")
If answer = vbYes Then
Call Roll_Up
Else
End If
Application.ScreenUpdating = True
End If
Else
MsgBox "No files selected"
End If
 

Attachments

  • error code.PNG
    error code.PNG
    8.9 KB · Views: 9

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
The error occurs since you are making an assumption. Your assumption is, that the COUNT of worksheets is related to the NAME of the worksheet. It's not!
You can test it yourself. Add some worksheets to your workbook by clicking the + and then delete all your sheets, except the first one and the last one. The first one could be named "Sheet1" and the last one could be named "Sheet7". If you've deleted all sheets in between, the COUNT of worksheets equals 2, but the second sheet in the collection of sheets is named "Sheet7".
Does this make sense?
 
Upvote 0

Forum statistics

Threads
1,215,794
Messages
6,126,944
Members
449,349
Latest member
Omer Lutfu Neziroglu

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