Problem after unprotecting a sheet

judgejustin

Board Regular
Joined
Mar 3, 2014
Messages
150
I am using the following code to insert some data and formatting. It works fine until I try to add protection to one of the sheets. The problem is the section of code that seems to be an issue shouldn't be effected, or at least I don't understand how it could be effected. The part that stops working is when the code pastes the formatting.
I need to unprotect another sheet in this code as well but I was only doing one at the time. Any help would be great. If I can get this one figured out I may need help with protection on the other sheet in the code as well. At this point I just don't know.
Code:
Sub Insert_Record()
'
' Insert_Record Macro
'

'
    ActiveWindow.SmallScroll Down:=22
    Range("D34:D49").Select
    Selection.Copy
    ActiveWindow.SmallScroll Down:=13
    Range("H54").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True
    Range("H54:AC54").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Inventory Sheet").Select
    Range("A5:V5").Select
    With Sheets("Inventory Sheet")
    .Unprotect "TEST"
    Selection.Insert Shift:=xlDown
 
   Selection.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats, Operation _
        :=xlNone, SkipBlanks:=False, Transpose:=False

    .Protect "TEST"
    End With
    Sheets("New Record Entry").Select
    ActiveWindow.SmallScroll ToRight:=-4
    ActiveWindow.SmallScroll Down:=-31
    Range("C27:F27").Select
    ActiveCell.FormulaR1C1 = ""
    Range("F24").Select
    ActiveCell.FormulaR1C1 = ""
    Range("C24").Select
    ActiveCell.FormulaR1C1 = ""
    Range("F20").Select
    ActiveCell.FormulaR1C1 = ""
    Range("C20").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D16").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D14").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D12").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D10").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D8").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D4").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D6:E6").Select
    ActiveCell.FormulaR1C1 = ""
    Range("D4").Select
End Sub
 
Last edited:
Does this help?

VBA Code:
Sub Insert_Record()
'Set a constant for password to save having to keep
'typing it it also makes it easier to maintain
Private Const pw As String = "TEST"
'
' just unprotect all the sheets you need to here
' then protect once the macro has finished executing
Sheets("Inventory Sheet").Unprotect pw
Sheets("New Record Entry").Unprotect pw
    
    
    ' Inserting a row can be done out of sequence
    
    Sheets("Inventory Sheet").Range("A5:V5").Insert Shift:=xlDown
    
    'We can save on processing by not skipping back and forth
    With Sheets("New Record Entry")
        .Range("H54:AC54").Value = Transpose(.Range("D34:D49").Value)
        .Range("H54:AC54").Copy Sheets("Inventory Sheet").Range("A5:V5")
        .Range("C27:F27,F24,C24,F20,C20,D16,D14,D12,D10,D8,D4,D6:E6").ClearContents
    End With
    
Sheets("New Record Entry").Protect pw
Sheets("Inventory Sheet").Protect pw

End Sub
 
Upvote 0

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