If cell contains specific text, and is double clicked, activate macro

pontiff

Board Regular
Joined
Jun 11, 2009
Messages
143
Office Version
  1. 2016
Hi, this might sound a bit unusual but here goes... could vba be used to do the following?
If a cell on the sheet contains the text “Total”, when double clicked...
1 Copy the row above and paste it one row up
2. Insert new row below newly pasted row.

Thanks in advance.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
there is not DBL-CLICK event for cells.
put the macro on the quick access toolbar, and it can run the current row the cursor is on.
 
Upvote 0
Does this do what you want?
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If InStr(1, Target.Value, "Total") > 0 Then
    If Target.Row > 2 Then
        Application.EnableEvents = False
        With Target.Offset(-1, 0).EntireRow
            .Copy Destination:=Target.Offset(-2, 0).EntireRow
            .Insert
        End With
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
Solution
Does this do what you want?
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If InStr(1, Target.Value, "Total") > 0 Then
    If Target.Row > 2 Then
        Application.EnableEvents = False
        With Target.Offset(-1, 0).EntireRow
            .Copy Destination:=Target.Offset(-2, 0).EntireRow
            .Insert
        End With
        Application.EnableEvents = True
    End If
End If
End Sub
Thank for this, I’ll try it first thing tomorrow and let you know. Much appreciated!
 
Upvote 0
What version of Excel do you run? Please update your profile.
 
Upvote 0
Thank for this, I’ll try it first thing tomorrow and let you know. Much appreciated!
Apologies, I thought I’d replied to this. It works great but due to a change in my design I was wondering if it can select the first 9 cells of the row rather than the entire row and do the same job. Thanks for posting, much appreciated.
 
Upvote 0
Apologies, I thought I’d replied to this. It works great but due to a change in my design I was wondering if it can select the first 9 cells of the row rather than the entire row and do the same job. Thanks for posting, much appreciated.
I assume you mean limit the copy/paste and insertion of a new row to the first 9 columns of any data row. Below assumes that the first 9 columns are columns A:I.

Replace the existing code with this:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column > 9 Then Exit Sub
If InStr(1, Target.Value, "Total") > 0 Then
    If Target.Row > 2 Then
        Application.EnableEvents = False
        With Target.Offset(-1, -Target.Column + 1).Resize(1, 9)
            .Copy Destination:=Target.Offset(-2, -Target.Column + 1)
            .Insert
        End With
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
I assume you mean limit the copy/paste and insertion of a new row to the first 9 columns of any data row. Below assumes that the first 9 columns are columns A:I.

Replace the existing code with this:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column > 9 Then Exit Sub
If InStr(1, Target.Value, "Total") > 0 Then
    If Target.Row > 2 Then
        Application.EnableEvents = False
        With Target.Offset(-1, -Target.Column + 1).Resize(1, 9)
            .Copy Destination:=Target.Offset(-2, -Target.Column + 1)
            .Insert
        End With
        Application.EnableEvents = True
    End If
End If
End Sub
Thanks so much for this, very much appreciate all the help I get and how much I've learnt from this
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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