我曾经使用以下代码在点击A11:A29或G8时调出用户表单(日历):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Target, Range("A11:A29")) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, Range("G8")) Is Nothing Then DatePickerForm.Show
End Sub
由于MS MonthView Control 6.0的问题,我不得不废弃该用户表单。我移动到找到here的其他日历。该网站建议在工作表上的任意位置双击以显示用户表单。这按设计工作,但对任何单元格都有效,而我想通过双击或单击一次将其限制为仅单元格A11:A29和G8。我尝试将页面上的代码修改为:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ARange As Range
Dim GRange As Range
Set ARange = Range("A11:A29")
Set GRange = Range("G8")
If Not Application.Intersect(Target, ARange) Is Nothing Then DatePickerForm.Show
If Not Application.Intersect(Target, GRange) Is Nothing Then DatePickerForm.Show
End Sub
我得到运行时错误91,对象变量或未设置块。调试突出显示" if isdate"以下行:
Private Sub UserForm_Activate()
If IsDate(Target.Value) Then
Calendar1.Value = Target.Value
End If
Call MoveToTarget
End Sub
答案 0 :(得分:1)
这是因为Target
Range
对象只在它所放置的特定工作表代码模块中很熟悉(就像一个局部变量),因此在DatePickerForm
表单模块中无法识别它。
使用:
Private Sub UserForm_Activate()
If IsDate(ActiveCell.Value) Then Calendar1.Value = ActiveCell.Value
End If
,有一种更有效的方法可以查看Target
是否属于多个Range
,使用Union
合并多个Range
进入一个Range
,请参阅以下代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim UnionRange As Range
Set UnionRange = Application.Union(Range("A11:A29"), Range("G8"))
If Not Application.Intersect(Target, UnionRange) Is Nothing Then DatePickerForm.Show
End Sub