代码返回错误消息,暗示代码的某些部分丢失,但它们仍然存在。例如,没有结束,当然它就在那里。
这是我分别编写的两个代码,目的是在确保它们运行良好后进行组合。但事实证明,我需要一些帮助才能让他们聚在一起。
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.EnableEvents = False
Dim k As Integer
Dim j As Integer
If Target.Column = 4 Then
confirm = MsgBox("Do you wish to confirm entry of this data?" _
& vbCrLf & "You'll not be allowed to change it!", vbYesNo, "confirm Entry")
Select Case confirm
Case Is = vbYes
Dim Cell As Range
With ActiveSheet
.Unprotect Password:="10"
.Cells.Locked = False
For Each Cell In ActiveSheet.UsedRange
For j = 4 To 4
For k = 10 To 60
If Cells(k, j).Value <> "" Then
Cells(k, "C").Value = Date
Cells(k, "C").NumberFormat = "m/d/yyyy"
Else
Cell.Locked = True
End If
Next
.Protect Password:="10"
Case Is = vbNo
Next
Next
Range("C11:C60").Columns.AutoFit
End With
End If
End Select
Application.EnableEvents = True
End Sub
我正在尝试让此代码自动执行以下操作:当单元格为空时,可以输入信息,然后在数据在单元格中后在相邻单元格中显示日期。当数据需要更改时,请求具有是/否vba功能的密码。
我清理了你的代码,虽然还没有测试它是否做你想要它做的事情,但它至少编译:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Dim k As Integer
Dim confirm As Integer
Dim cell As Range
If Target.Column = 4 Then
confirm = MsgBox("Do you wish to confirm entry of this data?" _
& vbCrLf & "You'll not be allowed to change it!", vbYesNo, "confirm Entry")
Select Case confirm
Case vbYes
With Me
.Unprotect Password:="10"
.Cells.Locked = False
For k = 10 To 60
If .Cells(k, 4).Value <> "" Then
.Cells(k, 3).Value = Date
.Cells(k, 3).NumberFormat = "m/d/yyyy"
End If
Next
.Cells.Locked = True
.Protect Password:="10"
.Range("C11:C60").Columns.AutoFit
End With
End Select
End If
Application.EnableEvents = True
End Sub