Double Click VBA Code Deletes Range in Formula

Malcolm torishi

Board Regular
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
 

Peter_SSs

MrExcel MVP, Moderator
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top