Unprotect Sheet Then Protect Again

L

Legacy 436357

Guest
Hello,

I have this code to put data on the 'Service Log'! sheet. I want to keep that sheet protected but the code won't run unless I keep the sheet unprotected.

Can there be lines added so it will unprotect the 'Service Log'! and protect again please?

Thank you

Code:
Sub CopyPasteRows1()

    Dim lr As Long
    Dim r As Long
    Dim nr As Long
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column on Sheet3
    lr = Sheets("2011 Ford F150").Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through all rows starting on row 2 on Sheet2
    For r = 2 To lr
        If Sheets("2011 Ford F150").Cells(r, "P") = True Then
'           Find next available row on Sheet2
            nr = Sheets("Service Log").Cells(Rows.Count, "C").End(xlUp).Row + 1
'           Copy data to from columns C-K to Sheet2
            Sheets("2011 Ford F150").Range("C" & r).Resize(, 9).Copy
'           Paste values from columns C-K to Sheet2
            Sheets("Service Log").Cells(nr, "C").PasteSpecial xlPasteValues
'           Remove checkbox from Sheet3
            Sheets("2011 Ford F150").Cells(r, "P") = False
                       
        End If
    Next r
    
'   Clears colums H,I, and K after code runs

        Worksheets("2011 Ford F150").Range("H7:I40").ClearContents
        Worksheets("2011 Ford F150").Range("K7:K40").ClearContents
            
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    MsgBox "Service entries have been posted to 'Service Log'!"
    
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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