Double Click VBA Code Deletes Range in Formula

Malcolm torishi

Board Regular
Joined
Apr 26, 2013
Messages
219
I have the following vba code in a worksheet that when you double click on say the 5th row under the 4th column (D) it deletes everything in that row from the 3rd column (C) to the 1st column (A) and then cuts and pastes the person’s name in the 1st column (A) onto another sheet. Works perfectly.
The problem I have is, I also have the following formula =counta(A5:Ab25) in cell A1 counting the number of people in column A from rows 5 to 25 , and what happens is when I double click on a cell under the 4th column (D) to delete, cuts and paste the person’s name on that row onto my another sheet, it also reduces my range in my =counta formula, so each time I double click on a row under the 4th column my range reduces in size by one to say =counta(A5:A19)
Can anyone explain why and how to keep my formula working by not reducing the range in my counta formula each time I double click

Thank you

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Modified 6/18/2019 3:09 PM EDT
If Target.Column = 4 And Target.Row > 1 Then
Cancel = True
Dim Lastrow As Long
Lastrow = Sheets(4).Cells(Rows.Count, "B").End(xlUp).Row + 1
Target.Offset(, -3).Resize(, 1).Copy Sheets(4).Cells(Lastrow, 2)
Target.Offset(, -3).Resize(, 3).Delete
End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Can anyone explain why ..
Because when you delete those 3 cells, they are replaced by moving all the cells below up one and introducing 3 new cells at the bottom of the sheet. It is exactly the same as selecting, say, A13:C13, right click and choose Delete... -> (Shift cells up) ->:OK

Can anyone explain ... how to keep my formula working by not reducing the range in my counta formula each time I double click
Try making this change in your code
Rich (BB code):
Target.Offset(, -3).Resize(, 3).Clear
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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