Unprotect copied protected sheets and paste a range as value

moshc

New Member
Joined
Oct 24, 2019
Messages
6
Hi All I have this code below,

I'm trying to figure out how i can copy protected sheets and unprotect together with copying a specific range and pasting it as value only.

Hoping for a quick response. Thank you! ?


Code:
Sub CopySheets()    Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, srcWB As Workbook, desWB As Workbook
    Set desWB = ThisWorkbook
    
    On Error Resume Next
    ActiveWorkbook.Unprotect Password:="sky1212"
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder."
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\"
    End With
    MyFile = Dir(MyFolder)
    Do While MyFile <> ""
        Set srcWB = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
        With srcWB
            .Sheets(1).Copy desWB.Sheets(desWB.Sheets.Count)
            With ActiveSheet.UsedRange
                .Cells.Validation.Delete
                .Cells.Value = .Cells.Value
            End With
            .Close False
        End With
        MyFile = Dir
    Loop
    Application.ScreenUpdating = True
Worksheets("Sheet1").Visible = xlSheetHidden
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello. Missing a little info but I think I understand.

To just use the 'Unprotect' method ensuring to include a password if it needs it
To paste values you need to use PasteSpecial as below
This should point you in the right direction.



Code:
Sub CopySheets()
Application.ScreenUpdating = False
    Dim MyFolder As String, MyFile As String, srcWB As Workbook, desWB As Workbook
    Set desWB = ThisWorkbook
    
    On Error Resume Next
    ActiveWorkbook.Unprotect Password:="sky1212"
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder."
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\"
    End With
    MyFile = Dir(MyFolder)
    Do While MyFile <> ""
        Set srcWB = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
        With srcWB
            .Sheets(1).Unprotect "password"
            .Sheets(1).Cells.Copy 'copy all the cells.
            desWB.Sheets(desWB.Sheets.Count).Cells(1, 1).PasteSpecialxlPasteValues 'only paste values starting at A1
            With ActiveSheet.UsedRange
                .Cells.Validation.Delete
                .Cells.Value = .Cells.Value
            End With
            .Sheets(1).Protect "password"
            .Close False
        End With
        MyFile = Dir
    Loop
    Application.ScreenUpdating = True
Worksheets("Sheet1").Visible = xlSheetHidden
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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