我正在尝试完成此操作,以便可以单击ws2(D11)和ws3(C12)单元格并显示一个包含ClientNumber的框。我只是无法理解它是如何工作的,因为我试图为我的代码改编一些解决方案,但是没有成功,也不知道为什么。
Public ClientNumber As String
Sub Booker()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Set ws = Sheets("ClientEnd")
Set ws2 = Sheets("Booking End")
Set ws3 = Sheets("Training End")
Dim ClientName As String
ClientName = InputBox("Please enter your name")
ClientNumber = InputBox("Please enter a contact number")
ws2.Range("D11") = ClientName
ws3.Range("C12") = ClientName
ws2.Range("O11:R11").Style = "Good"
ws3.Range("H12:K12").Style = "Good"
ws3.Range("I12").Style = "Normal"
Call NumberBox
End Sub
Sub NumberBox()
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Set ws = Sheets("ClientEnd")
Set ws2 = Sheets("Booking End")
Set ws3 = Sheets("Training End")
If Target.Address = ws2.Range("$D$11") Then MsgBox ClientNumber
If Target.Address = ws3.Range("$C$12") Then MsgBox ClientNumber
End Sub
当我单击每张表中的给定单元格时,我希望在消息中弹出ClientNumber,这些单元格会成功更新为ClientName,但是什么也没有发生。没有错误,没事。
答案 0 :(得分:1)
您可以使用事件Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
。
在ws3中放入
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("C12")) Is Nothing Then 'range than u want to capture
Call NumberBox 'your code
End If
End Sub
对ws2做同样的事情
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("D11")) Is Nothing Then 'range than u want to capture
Call NumberBox 'your code
End If
End Sub
您必须双击该单元才能运行事件
答案 1 :(得分:0)
您可以尝试将SelectionChange事件添加到工作表中。在工作表2和3上添加代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "&D&11" Then 'Change the "&D&11" to "&C&12" for worksheet 3
Call NumberBox ' You could also add Target as a paramater
End If
End Sub