Combining two VBA functions

GManbright

New Member
Joined
Jan 22, 2023
Messages
1
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good afternoon everyone. I have the following two VBA functions and need help combining them so that I can use both in the same workbook. I would really appreciate it if someone can help me with this. Thanks so much!

FIRST FUNCTION:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim rng As Range, r As Range
    Set sh1 = ThisWorkbook.Sheets("Sheet1")
    Set sh2 = ThisWorkbook.Sheets("Sheet2")
    If Target.Address = "$A$2" Then
        Set rng = sh2.Range("A:A").Find(Target.Value)
        If Not rng Is Nothing Then
            Set r = rng
            Do
                If r.Address = rng.Address Then
                    r.EntireRow.Copy sh1.Range("A" & Target.Row + 1)
                Else
                    r.EntireRow.Copy sh1.Range("A" & Target.Row + r.Row - rng.Row + 1)
                End If
                Set r = sh2.Range("A:A").FindNext(r)
            Loop Until r.Address = rng.Address
        End If
    Else
        Set rng = sh1.Range("A:A").Find(Target.Value)
        If Not rng Is Nothing Then
            Set r = rng
            Do
                If r.Address = rng.Address Then
                    r.EntireRow.Delete
                Else
                    r.EntireRow.Delete
                End If
                Set r = sh1.Range("A:A").FindNext(r)
            Loop Until r Is Nothing
        End If
    End If
End Sub


SECOND FUNCTION:

VBA Code:
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range)
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim col2 As Integer, col1 As Integer
    Set sh1 = ThisWorkbook.Sheets("Sheet1")
    Set sh2 = ThisWorkbook.Sheets("Sheet2")
    If Target.Address = "$B$3" Then
        col2 = Application.Match(sh1.Range("B32").Value, sh2.Range("B1:AB1"), 0)
        If Not IsError(col2) Then
            sh2.Range(sh2.Cells(2, col2), sh2.Cells(2, col2 + 100)).EntireColumn.Copy
            sh1.Range("B3").Insert Shift:=xlToRight
        End If
    Else
        col1 = Application.Match(Target.Value, sh1.Range("B1:AB1"), 0)
        If Not IsError(col1) Then
            sh1.Range(sh1.Cells(3, col1), sh1.Cells(3, col1 + 100)).EntireColumn.Delete
        End If
    End If
End Sub


Thanks in advance!
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
VBA Code:
....
....
if target address="this" then
'do something
end if
if target.address = "that" then
'do something else
end if
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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