Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

Excel VBA Error On Unlocked Cells When Sheet is Protected

I have a large database of contacts and would like to lock and protect the header rows (1:5). The rest remain unlocked so that the user can edit them as they wish.

When I Protect the Worksheet, I'm allowing the users to only:

  • Select unlocked cells
  • Format columns (widen as they need to display entered info)
  • Insert rows
  • Delete rows
  • Sort

I don't want them to be able to insert columns as it messes with the macros. They can only insert rows that are below the header.

All of the existing macros work when protected except one. Using a double left-click of the cell next to the contacts name places a check mark or 'X' and conditionally highlights their information accordingly as having been selected or not selected:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    'Limit Target count to 1
    If Target.Count > 1 Then Exit Sub
    'Isolate Target to a specific range
    If Intersect(Target, Range("SelectionMaster")) Is Nothing Then Exit Sub
    'set Target font tp "marlett"
    Target.Font.Name = "marlett"
    'Check value of target
    If Target.Value < "a" Then
        Target.Value = "a" 'Sets target Value = "a"
        Cancel = True
        Exit Sub
    End If
    If Target.Value = "a" Then
        Target.Value = "r"
        Cancel = True
        Exit Sub
    End If
    If Target.Value = "r" Then
        Target.ClearContents 'Sets Target Value = ""
        Cancel = True
        Exit Sub
    End If
    End Sub

The first time it's used after protecting, it brings up an error on Target.Font.Name = "marlett" and I don't understand why. After the initial error, double clicking only lets you edit the cell.

I appreciate the help!

question from:https://stackoverflow.com/questions/66053358/excel-vba-error-on-unlocked-cells-when-sheet-is-protected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...