调试发现错误 91 与 intersect(target, [range variable]).value
Debug is finding an error 91 with an intersect(target, [range variable]).value
供参考:
设备-MacBook;我通常使用 windows。我没有遇到任何兼容性问题。
OS- 大苏尔 v11.5.2
Excel 版本- 16.60
文件类型- xlsm
操作详情:
我需要列范围 D4:D26 来输入日期戳,当任何值被输入到列 C 中的相应单元格时。
问题:
我有这个代码-
Private Sub Worksheet_Change(ByVal Target As Range)
Dim EntryDate As String
Dim rngA As Range 'Declared variable
Set rngA = Range("D:D") 'Set Declared variable
If Intersect(Target, rngA).Value <> "" Then 'This line is displaying the runtime 91 code
EntryDate = Format(Now, "dddd, mmm dd, yyyy")
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = EntryDate 'When I comment this line out I get no runtime errors
End If
End Sub
- 当我在 C 列中输入值时,出现了我预期的结果。 D 列确实输出日期戳。而且还 returns 运行时错误 91.
补充说明:
我去过这个平台内的多个论坛和许多相关问题。尝试使用新的工作簿和工作表;我还尝试为变量对象指定当前工作簿和工作表,并且仍然在同一有问题的行指定运行时 91 returns。我做错了什么关于我的 Intersect 语法?是 <> 引起的吗?
两个紧迫的问题:
- 您需要先测试
If Not Intersect(Target, rngA) Is Nothing
。也就是说,您需要测试 Target
和 D 列是否相交, 在您尝试使用 .Value
. 之前
- 您在
Worksheet_Change
事件处理程序中修改工作表,导致事件再次触发。通常使用 Application.EnableEvents = False
来避免这种情况。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Me.Range("D:D"))
If Not rng Is Nothing Then
Dim entryDate As String
entryDate = Format$(Now, "dddd, mmm dd, yyyy")
OnError GoTo SafeExit
Application.EnableEvents = False
Dim cell As Range
For Each cell In rng
If Not IsEmpty(cell) Then
cell.Offset(,1).Value = entryDate
End If
Next
End If
SafeExit:
Application.EnableEvents = True
End Sub
供参考:
设备-MacBook;我通常使用 windows。我没有遇到任何兼容性问题。
OS- 大苏尔 v11.5.2
Excel 版本- 16.60
文件类型- xlsm
操作详情:
我需要列范围 D4:D26 来输入日期戳,当任何值被输入到列 C 中的相应单元格时。
问题:
我有这个代码-
Private Sub Worksheet_Change(ByVal Target As Range)
Dim EntryDate As String
Dim rngA As Range 'Declared variable
Set rngA = Range("D:D") 'Set Declared variable
If Intersect(Target, rngA).Value <> "" Then 'This line is displaying the runtime 91 code
EntryDate = Format(Now, "dddd, mmm dd, yyyy")
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = EntryDate 'When I comment this line out I get no runtime errors
End If
End Sub
- 当我在 C 列中输入值时,出现了我预期的结果。 D 列确实输出日期戳。而且还 returns 运行时错误 91.
补充说明: 我去过这个平台内的多个论坛和许多相关问题。尝试使用新的工作簿和工作表;我还尝试为变量对象指定当前工作簿和工作表,并且仍然在同一有问题的行指定运行时 91 returns。我做错了什么关于我的 Intersect 语法?是 <> 引起的吗?
两个紧迫的问题:
- 您需要先测试
If Not Intersect(Target, rngA) Is Nothing
。也就是说,您需要测试Target
和 D 列是否相交, 在您尝试使用.Value
. 之前
- 您在
Worksheet_Change
事件处理程序中修改工作表,导致事件再次触发。通常使用Application.EnableEvents = False
来避免这种情况。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Me.Range("D:D"))
If Not rng Is Nothing Then
Dim entryDate As String
entryDate = Format$(Now, "dddd, mmm dd, yyyy")
OnError GoTo SafeExit
Application.EnableEvents = False
Dim cell As Range
For Each cell In rng
If Not IsEmpty(cell) Then
cell.Offset(,1).Value = entryDate
End If
Next
End If
SafeExit:
Application.EnableEvents = True
End Sub