我有一个奇怪的问题,我在excel 2013中有一张表,它使用双击来选择一个单元格(其中有一些我想要锁定的文本,因此用户不会更改),更改文本颜色以突出显示选择单元格,然后根据行中的target.column放置一个值。
如果我解锁工作表但是所有锁定工作表的尝试最终都会出现“Target.row”值的错误,那么它工作正常我将msg语句放入其中我可以看到每次调用结束时然后,活动单元格移动到下一个未锁定的单元格,然后在下次单击要突出显示的所需单元格时不会移动/更新值。
我为此尝试了各种修复,并进行了许多代码更改。目前代码是:
Private Sub Workbook_Open()
' protect worksheets but allow macros
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:="Purple15", userinterfaceonly:=True
Next ws
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Sheet3.Protect Password:="Purple15", UserInterFaceOnly:=True
Dim i As Integer
Dim d As Integer
MsgBox " row is " & Target.Row & " col is " & Target.Column
'Cells(Target.Row, 1).Value = "S"
For d = 1 To 1000
Next d
' above just slows it down to see effect
Sheet3.Unprotect Password:="Purple15"
'Cells(Target.Row, 1).Value = "U"
' check if double click is in the range cols 9 to 13 as these are the only ones they should choose
' if so then set font colour to red for the chosen cell and put chosen col number in col 16 for the sheet to then pick up from
'Cells(5, 1).Value = "U"
If Target.Column >= 9 And Target.Column <= 13 Then
'Cells(Target.Row, 1).Value = 2
Cells(Target.Row, 9).Font.Color = vbBlack
Cells(Target.Row, 10).Font.Color = vbBlack
Cells(Target.Row, 11).Font.Color = vbBlack
Cells(Target.Row, 12).Font.Color = vbBlack
Cells(Target.Row, 13).Font.Color = vbBlack
Target.Font.Color = vbRed
Cells(Target.Row, 16) = (Target.Column)
' also check if the double click is in the reset box, if so then reset all the values in col 16 to the starting condition of 9
ElseIf Target.Column = 2 And Target.Row = 3 Then
'Cells(3, 1).Value = 3
For i = 8 To 300
Cells(i, 1).Value = " "
Cells(i, 9).Font.Color = vbBlack
Cells(i, 10).Font.Color = vbBlack
Cells(i, 11).Font.Color = vbBlack
Cells(i, 12).Font.Color = vbBlack
Cells(i, 13).Font.Color = vbBlack
If Cells(i, 16).Value >= 10 Then Cells(i, 16).Value = 9
Next i
Else
End If
'Cells(5, 1).Value = " "
'Cancel = True
Sheet3.Protect Password:="Purple15", userinterfaceonly:=True
'Cells(5, 1).Value = "L"
End Sub
答案 0 :(得分:0)
您可能希望了解如何使用按钮。这样您就不必担心最终用户更改值(很容易)。我创建了一个只有一行的电子表格,A列中有“SomeText 1”,B列中有“SomeText 2”,C列中有“SomeText 3”。下面是代码,我认为它按预期工作。
在“ThisWorkbook”中,我使用了您的确切代码:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Protect Password:="Purple15", userinterfaceonly:=True
Next ws
End Sub
在“Sheet1”中我使用了这个:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column >= 1 And Target.Column <= 3 Then
'Reset all of the "Buttons" to black
Cells(Target.Row, 1).Font.Color = vbBlack
Cells(Target.Row, 2).Font.Color = vbBlack
Cells(Target.Row, 3).Font.Color = vbBlack
'Makes the one they clicked red
Target.Font.Color = vbRed
'Change focus so you don't get the "Protected Worksheet"
' warning after a double-click
Cells(Target.Row, 6).Select
'Unlock the cell you're editing
Cells(Target.Row, 6).Locked = False
'Set the cell's value
Cells(Target.Row, 6) = (Target.Column)
'Lock it again
Cells(Target.Row, 6).Locked = True
End If
End Sub
这就是你要找的东西吗?