Excel 如何在一张工作表中多次双击事件之前使用
我的假设工作表X有以下三个代码Excel 如何在一张工作表中多次双击事件之前使用,excel,vba,excel-formula,Excel,Vba,Excel Formula,我的假设工作表X有以下三个代码 第一代码 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim rFound As Range, vFind If Target.Column = 3 Then Cancel = True vFind = Target On Error Resume Next With Sheet4.Columns(3)
- 第一代码
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim rFound As Range, vFind If Target.Column = 3 Then Cancel = True vFind = Target On Error Resume Next With Sheet4.Columns(3) Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _ LookIn:=xlValues, LookAt _ :=xlWhole, SearchOrder:=xlByRows) End With On Error GoTo 0 If Not rFound Is Nothing Then Application.Goto rFound Else MsgBox "No match for " & vFind & " on " & Sheet4.Name End If End If End Sub
- 第二代码
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim rFound As Range, vFind If Target.Column = 2 Then Cancel = True vFind = Target On Error Resume Next With Sheet5.Columns(2) Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _ LookIn:=xlValues, LookAt _ :=xlWhole, SearchOrder:=xlByRows) End With On Error GoTo 0 If Not rFound Is Nothing Then Application.Goto rFound Else MsgBox "No match for " & vFind & " on " & Sheet5.Name End If End If End Sub
- 第三代码
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim str As String Dim cboTemp As OLEObject Dim ws As Worksheet Set ws = ActiveSheet Set cboTemp = ws.OLEObjects("ComboBox1") On Error Resume Next With cboTemp 'clear and hide the combo box .ListFillRange = "" .LinkedCell = "" .Visible = False End With On Error GoTo errHandler If Target.Validation.Type = 3 Then 'if the cell contains 'a data validation list Cancel = True Application.EnableEvents = False 'get the data validation formula str = Target.Validation.Formula1 str = Right(str, Len(str) - 1) With cboTemp 'show the combobox with the list .Visible = True .Left = Target.Left .Top = Target.Top .Width = Target.Width + 5 .Height = Target.Height + 5 .ListFillRange = str .LinkedCell = Target.Address End With cboTemp.Activate 'open the drop down list automatically Me.ComboBox1.DropDown End If errHandler: Application.EnableEvents = True Exit Sub End Sub
在这里,我有三个事件在一张纸上双击事件,但我们知道在一张纸上不允许使用相同的宏名称,所以你们能在这方面帮助我吗?我想宏合并是唯一的选择,但我是vba的初学者,所以我真的不知道这一点,所以,任何帮助将不胜感激。提前感谢您。在保留相似部分的同时,将不同部分合并到Select Case语句中似乎并不太难
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("B:C")) Is Nothing Then
Cancel = True
Dim rFound As Range, vFind As Variant
'small bit of error control
if isempty(target) then exit sub
vFind = Target.value
On Error Resume Next
Select Case Target.Column
Case 2
With Sheet5.Columns(2)
Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole)
End With
Case 3
With Sheet4.Columns(3)
Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole)
End With
End Select
On Error GoTo 0
If Not rFound Is Nothing Then
Application.Goto rFound
Else
MsgBox "No match for " & vFind & " on " & _
iif(target.column = 3, Sheet4.Name, Sheet5.Name)
End If
End If
End Sub
在保留相似部分的同时,将不同的部分组合到Select Case语句中似乎并不太难
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("B:C")) Is Nothing Then
Cancel = True
Dim rFound As Range, vFind As Variant
'small bit of error control
if isempty(target) then exit sub
vFind = Target.value
On Error Resume Next
Select Case Target.Column
Case 2
With Sheet5.Columns(2)
Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole)
End With
Case 3
With Sheet4.Columns(3)
Set rFound = .Find(What:=vFind, After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole)
End With
End Select
On Error GoTo 0
If Not rFound Is Nothing Then
Application.Goto rFound
Else
MsgBox "No match for " & vFind & " on " & _
iif(target.column = 3, Sheet4.Name, Sheet5.Name)
End If
End If
End Sub
您已经合并了宏。太好了,非常感谢你们拨出宝贵的时间解决问题。你们已经合并了宏。伟大的非常感谢您分配宝贵的时间并解决问题。如果我想在一张工作表中使用两个不同的宏来双击事件,该怎么办?将第二个第三个双击事件更改为子例程,然后从第一个双击开始调用它们。如果我想在一张工作表中使用两个不同的宏来双击事件,该怎么办?更改第二个第三个双击子例程中的事件,然后从第一次双击开始调用它们。