Using the menu from the right mouse button:
1. ThisWorkbook module
Option Explicit
Private Sub Workbook_Deactivate()
Dim ctrl As CommandBarControl
On Error Resume Next
For Each ctrl In Application.CommandBars("Cell").Controls
If ctrl.Tag = "Delete_after" Then ctrl.Delete
Next
End Sub
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim ctrl As CommandBarControl
Dim myPopUp As CommandBarButton
If TypeName(Sh) = "Worksheet" Then
On Error Resume Next
For Each ctrl In Application.CommandBars("Cell").Controls
If ctrl.Tag = "Delete_after" Then ctrl.Delete
Next
On Error GoTo the_end
Set myPopUp = Application.CommandBars("Cell").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
With myPopUp
.Caption = "Inserting rows in selection areas"
.OnAction = "insert_rows"
.FaceId = 133
.Tag = "Delete_after"
End With
Set myPopUp = Nothing
Application.CommandBars("Cell").Controls(2).BeginGroup = True
End If
Exit Sub
the_end: MsgBox "Error - End", vbOKOnly, "Info"
End Sub
2. Standard module, Module1:
Option Explicit
Option Private Module
Sub insert_rows()
Dim nmbrrws As Long, i As Long, nmbrArea As Long
On Error Resume Next
nmbrrws = Abs(CLng(InputBox("Enter the number of rows:", "How many rows to add ?", "1")))
If Err.Number <> 0 Then MsgBox "An error occured", vbOKOnly, "Info": Exit Sub
On Error GoTo the_end
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual
nmbrArea = Selection.Areas.Count
For i = nmbrArea To 1 Step -1
Rows(Selection.Areas(i).Cells(1).Row + 1 & ":" & Selection.Areas(i).Cells(1).Row + nmbrrws).Insert Shift:=xlDown
Next
Application.Calculation = xlAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
the_end: MsgBox "Error - End", vbOKOnly, "Info"
End Sub
Bookmarks