【发布时间】:2015-04-10 12:22:27
【问题描述】:
我有一个奇怪的问题,我在 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
【问题讨论】:
-
不清楚你在问什么。在您保护它之后,发布的代码对 Target.Row 没有任何作用。您发布的代码是否是产生错误的代码,如果是,错误是什么以及哪一行?