JohnGow383
Board Regular
- Joined
- Jul 6, 2021
- Messages
- 141
- Office Version
- 2013
- Platform
- Windows
I have some code which manipulates a text string in "A1" which works fine when the sheet is unprotected. When I protect the sheet using Excel I tickbox Format Cells and Edit Objects and the code still works fine. I need edit objects as I've other code which manipulates comments (notes). This is a long winded process which is easy to forget so I have code to quickly unprotect the sheet and protect using keyboard shortcuts. However, when I use this the formatting of the cells gives me a runtime error and I don't know why. I AllowFormattingCells:=True. Easier to show the code so here goes.
This is the text manipulation code. The first error is occuring on the first formatting part ("Bold Italic")
Here is the code I'm using to protect the worksheets
and here is the code to unprotect
Any ideas why it's not allowing formatting of cells? Thanks
This is the text manipulation code. The first error is occuring on the first formatting part ("Bold Italic")
VBA Code:
Sub CopyVoyNo() 'Copies Voyage Number from Userform13 - Does some fancy formatting. Ensure Worksheet is protected using Ctrl + Shift P and not from the button
'In order for this macro to work Protected worksheet needs to have enable edit objects and formatting cells
Dim ws As Worksheet
Dim i As Long
Dim x As Long
Dim z As Long
Dim lastchar As String
Set ws = ThisWorkbook.Worksheets("NOON Figs")
Application.ScreenUpdating = False
ws.Range("A1").Value = ws.Range("A33").Value
lastchar = Right(Range("A1"), 1)
i = InStr(Range("A1"), "V")
x = InStr(Range("A1"), ":") + 1
z = Len(Range("A1"))
With Cells(1, 1).Characters(i, 9).Font
.FontStyle = "Bold Italic"
End With
With Cells(1, 1).Characters(x, 8).Font
.Color = vbRed
.Size = 18
End With
Select Case lastchar
Case "L"
With Cells(1, 1).Characters(z, -1).Font
.FontStyle = "Bold"
.ColorIndex = 50
.Size = 19
End With
Case "B"
With Cells(1, 1).Characters(z, -1).Font
.FontStyle = "Bold"
.ColorIndex = 32
.Size = 19
End With
End Select
Application.ScreenUpdating = True
End Sub
Here is the code I'm using to protect the worksheets
VBA Code:
Sub ProtectSelectedWorksheets() 'Shortcut is Ctr + Shift P (Dont use on LOG entries until I can figure out how to allow formatting)
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)
'If Cancel is clicked
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:=True, Contents:=True, Scenarios:= _
True, AllowFormattingCells:=True, AllowFormattingColumns:=False, AllowFormattingRows:=False 'Protect each worksheet but enables edit objects (for Comment generating macros)
On Error GoTo 0
Next ws
sheetArray.Select
End Sub
and here is the code to unprotect
VBA Code:
Sub UnprotectAllWorksheets() 'Shortcut is Ctr + Shift U
'Create a variable to hold worksheets
Dim ws As Worksheet
'Create a variable to hold the password
Dim myPassword As Variant
'Set the password
myPassword = Application.InputBox(Prompt:="Enter password", _
Title:="Password", Type:=2)
'The User clicked Cancel
If myPassword = False Then Exit Sub
'Loop through each worksheet in the active workbook
For Each ws In ActiveWindow.SelectedSheets
'Protect each worksheet
ws.Unprotect password:=myPassword
Next ws
End Sub
Any ideas why it's not allowing formatting of cells? Thanks