我试图通过调用另一个宏来解锁工作表来使宏运行,以允许RFID阅读器扫描和输入数据,然后再次保护文档以使其无法编辑。
Private Sub Worksheet_Change(ByVal Target As Range)
' call another macro
Call UnProtect
' End Sub
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 3 And Target.Row <= 12 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "E").Value = "" Then
' Cells(Target.Row, "E").Value = Now()
' Cells(Target.Row, "E").NumberFormat = "h:mm AM/PM"
End If
End If
' Next
' Range("E:E").EntireColumn.AutoFit
' End Sub
' Private Sub Worksheet_Change(ByVal Target As Range)
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 1 And Target.Row <= 17 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "B").Value = "" Then
Cells(Target.Row, "B").Value = Now()
' Cells(Target.Row, "F").NumberFormat = "h:mm AM/PM"
End If
End If
' Next
' Range("F:F").EntireColumn.AutoFit
' End Sub
' Private Sub Worksheet_Change(ByVal Target As Range)
' Dim i As Integer
' MsgBox (Target.Row & ":" & Target.Column)
' For i = 8 To 200
If Target.Column = 3 And Target.Row >= 15 Then
If Cells(Target.Row, Target.Column).Value <> "" And Cells(Target.Row, "D").Value = "" Then
Cells(Target.Row, "D").Value = Now()
Cells(Target.Row, "D").NumberFormat = "mm/dd/yyy"
End If
End If
' Next
Range("D:D").EntireColumn.AutoFit
' End Sub
' call another macro
Call Protect
End Sub
它要调用的两个宏是
Sub UnProtect()
'Unprotect a worksheet
Sheets("Sign in Sheet").UnProtect
End Sub
和
Sub Protect()
'Protect a worksheet
Sheets("Sign in Sheet").Protect
End Sub
但是,当我仅包含解锁宏时,代码将正常工作。但是,当我添加保护宏时,会出现错误代码
运行时错误“ 1004”: 无法设置Range类的NumberFormat属性
它指向
Cells(Target.Row, "D").NumberFormat = "mm/dd/yyyy"
关于正在发生什么的任何想法。
答案 0 :(得分:1)
对于Target超过单个单元格并且Worksheet_Change极有可能试图在自身之上运行的情况,我看不出有任何规定。
在Target中的每个范围对象中循环并禁用事件触发器。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo safe_exit
Application.EnableEvents = False
Call Unprotect
Dim t As Range
If Not Intersect(Target, Range("A1:A17")) Is Nothing Then
For Each t In Intersect(Target, Range("A1:A17"))
If t.Value <> vbNullString And t.Offset(0, 1).Value <> vbNullString Then
t.Offset(0, 1) = Now
t.Offset(0, 1).NumberFormat = "h:mm AM/PM"
End If
Next t
End If
If Not Intersect(Target, Range("C1:C12")) Is Nothing Then
For Each t In Intersect(Target, Range("C1:C12"))
If t.Value <> vbNullString And t.Offset(0, 2).Value <> vbNullString Then
t.Offset(0, 2) = Now
t.Offset(0, 2).NumberFormat = "h:mm AM/PM"
End If
Next t
'I don't understand why column F comes into play here
' Range("F:F").EntireColumn.AutoFit
End If
If Not Intersect(Target, Range("C15:C999999")) Is Nothing Then
For Each t In Intersect(Target, Range("C15:C999999"))
If t.Value <> vbNullString And t.Offset(0, 1).Value <> vbNullString Then
t.Offset(0, 1) = Now
t.Offset(0, 1).NumberFormat = "mm/dd/yyy"
End If
Next t
Range("D:D").EntireColumn.AutoFit
End If
Call Protect
safe_exit:
Application.EnableEvents = True
End Sub