Issue with Allowing Formatting of Cells when Protected in VBA

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
Hi.
I have an event change macro that copies comments from one cell to another sheet. It's rather long winded and I'm not great with syntax but was able to generate most of the code to get it to what I want to do. I wanted the comments to be concatenated with the current date but all as text. The child macro does the following:
1. On the 2nd sheet it formats a cell to be text, then it looks at the first sheet date cell and says the date on the first sheet = the cell on the 2nd sheet with a text format of "mmm d, yyyy".
2. The comments from the 1st sheet are copied and pasted into another cell on the 2nd sheet.
3. Using a 3rd cell, I have a FormulaR1C1 to concatenate the two cells with a hypen in the middle.
4. This cell then is copied and values pasted into the main comment cell and then it goes back to the first sheet after deleting the working cells on the 2nd sheet from above.
5. The final comment is something like: May 23, 2022 - Comments blah blah etc.

This works fine until I protect the sheet with code. The code for the copying and pasting is as follows:

VBA Code:
Sub CopyDailyComment() 'copies any comments into Log Entries sheet with added date

Sheets("LOG Entries").Range("k2").NumberFormat = "@"
Sheets("LOG Entries").Range("k2") = Format(Sheets("NOON Figs").Range("B2"), "mmm d, yyyy")
   Sheets("NOON Figs").Select
   Range("H13:K14").Select
    Selection.Copy
    Sheets("LOG Entries").Select
    Range("L2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("M2").Select
    Application.CutCopyMode = False
    Selection.FormulaR1C1 = "=RC[-2]&"" - ""&RC[-1]"
     Range("M2").Select
    Selection.Copy
    Range("T13:W14").Select
    ActiveSheet.Paste Link:=True
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("K2:M2").ClearContents
    Sheets("NOON Figs").Select
    Application.CutCopyMode = False
   
End Sub

A possible solution would be to clean up the above code. I just couldn't find a way easily to copy a date into text without formatting the cell prior to the copy and paste.

I have the following macro to quickly protect my selected sheets. I have editobjects successfully enabled but when I trying to allow formatting it doesn't work. Here is my protect ws code:

VBA Code:
Sub ProtectSelectedWorksheets() 'Shortcut is Ctr + Shift P

Dim ws As Worksheet
Dim sheetArray As Variant
Dim myPassword As Variant

'Set the password - Please use 63360
myPassword = Application.InputBox(prompt:="Enter password", _
    Title:="Password", Type:=2)

'The User clicked Cancel
If myPassword = False Then Exit Sub

'Capture the selected sheets
Set sheetArray = ActiveWindow.SelectedSheets

'Loop through each worksheet in the active workbook
For Each ws In sheetArray

    On Error Resume Next
                  
     ws.Select   'Select the worksheet
       
    ws.Protect Password:=myPassword, DrawingObjects:=False, Contents:=True, Scenarios:= _
        True, AllowFormattingCells:=True 'Protect each worksheet but enables edit objects (for Comment generating macros)
 
    On Error GoTo 0
       
Next ws

sheetArray.Select

End Sub

Any help much appreciated. Thanks in advance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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