BeforeDoubleClick具有多个范围的事件

时间:2014-09-14 01:11:10

标签: vba events inputbox

下午好,

我正在尝试在工作表中使用BeforeDoubleClick事件,该工作表调用列L的输入框,提示用户输入他们的ID,另一个调用输入框,提示用户输入在J列中进行游览的团队成员的名称。我只是在L列中只有一个事件时才能正常工作,但在J列中添加输入框会不断返回"compile error: Argument not optional"。我目前的代码是:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

   On Error Resume Next
   enterUserName Target
   GuideName Target

End Sub

Sub enterUserName(ByVal Target As Range, Cancel As Boolean)

Dim x As Range, Y As Range

Set x = Target
Set Y = Range("L3:L300")

If Intersect(x, Y) Is Nothing Then Exit Sub
        Cancel = True
        t.Offset(0, 0).Value = InputBox(Prompt:="Please enter your User ID.")
End Sub

Sub GuideName(ByVal Target As Range, Cancel As Boolean)

Dim t As Range, B As Range

Set t = Target
Set B = Range("J3:J300")

If Intersect(t, B) Is Nothing Then Exit Sub
Cancel = True
t.Offset(0, 0).Value = InputBox(Prompt:="Please enter ONLY the first name of the tour guide.")

End Sub 

非常感谢任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢你,马特 -

经过一些进一步的研究和故障排除后,代码正在利用BeforeDoubleClick事件为两个事件工作。最有用的信息来自Tushar Mehta的网站:http://www.tushar-mehta.com/publish_train/xl_vba_cases/1021_multiple_tasks_in_an_event_procedure.htm 我对网站上的最后一个例子进行了微小的修改,并将我的原始子修改为以下内容(并且一切都像我希望的那样工作):

Private Sub GoBeforeDoubleClick1(ByVal Target As Range, Cancel As Boolean)
Dim t As Range, B As Range
Set t = Target
Set B = Range("L3:L300")
If Intersect(t, B) Is Nothing Then
Exit Sub
Else: t.Offset(0, 0).Value = InputBox(Prompt:="Please enter your User ID.")
Exit Sub
End If
End Sub

Private Sub GoBeforeDoubleClick2(ByVal Target As Range, Cancel As Boolean)
Dim x As Range, Y As Range
Set x = Target
Set Y = Range("J3:J300")
If Intersect(x, Y) Is Nothing Then
Exit Sub
Else: x.Offset(0, 0).Value = InputBox(Prompt:="Please enter ONLY the first name of the tour guide.")
Exit Sub
End If
Cancel = True
End Sub


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
On Error Resume Next
GoBeforeDoubleClick1 Target, Cancel
GoBeforeDoubleClick2 Target, Cancel
Application.EnableEvents = True
End Sub