Combine 2 VBA codes in one module (double click to jump to another sheet)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello....

A few months ago, a member of this forum helped me with VBA code to double click a cell and jump to another sheet (the code is as below - the first code).
But then I need to modify this sheet so that not only the user can double click it from Column I but also from another column (column G).
I tried to modify the code (second code) and it worked (only seach for a partial match from another column)
My question is whether I can put these 2 codes in the same module?
I tried seaching it, and it seems that I can't put 2 codes in the same module. Can anyone help me how to combine these 2 codes together?

Thank you


The first code.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim rFound As Range
 
  If Not Intersect(Target, Range("I6:I31")) Is Nothing Then
    If Len(Target.Value) > 0 Then
      Cancel = True
      Set rFound = Sheets("Items").Columns("A").Find(What:=Target.Value, LookAt:=xlWhole)
      If rFound Is Nothing Then
        MsgBox Target.Value & " Not found"
      Else
        Application.Goto Reference:=rFound, Scroll:=True
        rFound.EntireRow.Select
      End If
    End If
  End If
End Sub


The second code.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim rFound As Range
 
  If Not Intersect(Target, Range("G6:G31")) Is Nothing Then
    If Len(Target.Value) > 0 Then
      Cancel = True
      Set rFound = Sheets("Items").Columns("W").Find(What:=Target.Value, LookAt:=xlPart)
      If rFound Is Nothing Then
        MsgBox Target.Value & " Not found"
      Else
        Application.Goto Reference:=rFound, Scroll:=True
        rFound.EntireRow.Select
      End If
    End If
  End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You could run them one after the other in the same module !
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim rFound As Range
 If Not Intersect(Target, Range("G6:G31")) Is Nothing Then
    If Len(Target.Value) > 0 Then
      Cancel = True
      Set rFound = Sheets("Items").Columns("W").Find(What:=Target.Value, LookAt:=xlPart)
      If rFound Is Nothing Then
        MsgBox Target.Value & " Not found"
      Else
        Application.Goto Reference:=rFound, Scroll:=True
        rFound.EntireRow.Select
      End If
    End If
  End If
  If Not Intersect(Target, Range("I6:I31")) Is Nothing Then
    If Len(Target.Value) > 0 Then
      Cancel = True
      Set rFound = Sheets("Items").Columns("A").Find(What:=Target.Value, LookAt:=xlWhole)
      If rFound Is Nothing Then
        MsgBox Target.Value & " Not found"
      Else
        Application.Goto Reference:=rFound, Scroll:=True
        rFound.EntireRow.Select
      End If
    End If
  End If
End Sub
 
Upvote 0
Solution
I see...... Thank you for your help.
I tried it and it works perfectly.....
 
Upvote 0

Forum statistics

Threads
1,224,889
Messages
6,181,605
Members
453,055
Latest member
cope7895

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top