Macro that runs when entire rows are deleted?

Pinball

Board Regular
Joined
Apr 18, 2002
Messages
107
Is there a macro that can run if it detects that a user deletes one or more entire rows? Something like the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If any entire row is deleted then
<rest of macro>
End If
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi Pinball.
As you maybe know, excel does not have such an event.
So, I thought anothe way using calculate event.
Pls, try this and hope this help.
Before you try this you need to de-active activesheet, and re-active the sheet.
(May be you will not be able to understand what I want to say, because of my bad English...)

<pre>
Private Sub Worksheet_Activate()
Cells(Rows.Count, Columns.Count).Formula = "=A1"
End Sub

Private Sub Worksheet_Calculate()
Dim strMsg As String
If IsEmpty(Cells(Rows.Count, Columns.Count)) Then
Application.ScreenUpdating = False
Rows(Rows.Count).Clear
Columns(Columns.Count).Clear
Cells(Rows.Count, Columns.Count).Formula = "=A1"
With Selection
If .Columns.Count = Columns.Count Then strMsg = "Rows "
If .Rows.Count = Rows.Count Then strMsg = "Columns "
End With
Application.ScreenUpdating = True
Call ShowMsgBox(strMsg)
End If
End Sub

Private Sub ShowMsgBox(str)
MsgBox str & "were deleted"
End Sub

</pre>
 
Upvote 0
Colo, Thanks for your response.
What are your thoughts towards my beginner's code below to accomplish my goal?
It seems to work..the only tiny disadvantage is that after deleting row(s) the user has to click on any cell for the macro to run (but I can certainly live with that!).
Question: Is it possible to revise this code so that I do not need to store the p value in a cell, but store it in memory instead?

Thanks.

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim p As Integer
p = ActiveSheet.UsedRange.Rows.Count
If p < Range("D1").Value Then
'Following line is a test formula
Range("a1").Formula = 1 + p
End If
Range("D1").Formula = p
End Sub
 
Upvote 0
Hi again.
-- quote-----------------------------
but store it in memory instead?
-------------------------------------

I guess, if you use public Variable.
But the best way is store it in a separate hidden worksheet as Tikas wrote.


By the way, if your version of xl is XL2000, try another way I thought.
Please paste this into sheet module.

<pre>

Dim R_Count As Long
Dim C_Count As Long

Private Sub Worksheet_Change(ByVal Target As Range)
Dim strMsg As String
If UsedRange.Rows.Count < R_Count Then strMsg = "Rows"
If UsedRange.Columns.Count < C_Count Then strMsg = "Columns"
If strMsg <> "" Then
MsgBox Target.Address & strMsg & " was deleted"
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
R_Count = UsedRange.Rows.Count
C_Count = UsedRange.Columns.Count
End Sub

</pre>
 
Upvote 0
Hi Pinball, Are you still watching here?
Here is another way.

<pre>
Option Explicit

Sub EventHack()
Dim lngId As Long
Dim CtrlCbc As CommandBarControl
Dim CtrlCbcRet As CommandBarControls

Set CtrlCbcRet = CommandBars.FindControls(ID:=478)
For Each CtrlCbc In CtrlCbcRet
CtrlCbc.OnAction = "DeletionMenuRouting"
Next CtrlCbc

Set CtrlCbcRet = CommandBars.FindControls(ID:=293)
For Each CtrlCbc In CtrlCbcRet
CtrlCbc.OnAction = "LineDeletionEventProcessingProc"
Next CtrlCbc
End Sub

Sub EventReset()
Dim lngId As Long
Dim CtrlCbc As CommandBarControl
Dim CtrlCbcRet As CommandBarControls

Set CtrlCbcRet = CommandBars.FindControls(ID:=478)
For Each CtrlCbc In CtrlCbcRet
CtrlCbc.OnAction = ""
Next CtrlCbc

Set CtrlCbcRet = CommandBars.FindControls(ID:=293)
For Each CtrlCbc In CtrlCbcRet
CtrlCbc.OnAction = ""
Next CtrlCbc
End Sub


Private Sub DeletionMenuRouting()
If Not TypeOf Selection Is Range Then Exit Sub
If Selection.Address = Selection.EntireRow.Address Then
Call LineDeletionEventProcessingProc
Else
Application.Dialogs(xlDialogEditDelete).Show
End If
End Sub

Private Sub LineDeletionEventProcessingProc()
MsgBox "Rows ware deleted"
Selection.Delete xlUp
End Sub
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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