'Macro on a button that delete the merge rows its on

Basically I have a form that insert 5 rows of data which span from column "A" to "V" with some merged. I had my macro which inserted this 5 rows insert a button at "V" and merge the 5 rows so that the button would land on all 5 rows. I need to give a function to this button so that it can delete all 5 rows of data. This function needs to take reference from where it is and delete the next 4 rows.

I don't really know excel VBA so I can only describe what I'm looking for.



Solution 1:[1]

You can count the number of rows already occupied in column A and then select the last row to delete it. In the code, you need to change what I have called "SheetName" to the actual name of your excel sheet.

Sub Delet_LastRow()
    
    'r = number of filled rows from A1 to A100000
    r = Application.WorksheetFunction.CountA(Worksheets("SheetName").Range("A1:A100000"))
    'Select the sheet named "SheetName"
    Sheets("SheetName").Select
    'select last 5 rows from column A to column V and delete them
    Range(Cells(r-5, "A").Address(), Cells(r, "V").Address()).Select
    Selection.Delete

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1