PitBoo,
Here is a different approach:
To add a row(s), select a cell and press ctrl+z. From the combobox that appears adjacent, select the number of rows you wish to enter.
To add cell(s) vertically, select a cell and press ctrl+Shift+Z. From the combobox that appears adjacent, select the number of rows you wish to enter.
Here are the 3 simple steps to follow:
Place 2 active x comboboxes on your worksheet named Combobox1 and Combobox2. Set both of their visible properties to false in the properties window. Their size and placement is totally irrelevant.
Place the following code in a standard module:
Public Sub InsertRow()
With ActiveSheet.ComboBox1
.Clear
For I = 1 To 10 'CHANGE TO THE MAX NUMBER OF POSSIBLE ADDED ROWS
.AddItem I
Next I
.Height = ActiveCell.Height
.Width = 50
.Left = ActiveCell.Offset(0, 1).Left
.Top = ActiveCell.Top
.Visible = True
End With
End Sub
Public Sub InsertCells()
With ActiveSheet.ComboBox2
.Clear
For I = 0 To 10 'CHANGE TO THE MAX NUMBER OF POSSIBLE ADDED CELLS
.AddItem I
Next I
.Height = ActiveCell.Height
.Width = 50
.Left = ActiveCell.Offset(0, 1).Left
.Top = ActiveCell.Top
.Visible = True
End With
End Sub
Place the following code in the sheet module:
Private Sub ComboBox1_Change()
With ComboBox1
For I = 1 To Val(.Value)
ActiveCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown
Next I
.Visible = False
End With
End Sub
Private Sub ComboBox2_Change()
With ComboBox2
For I = 0 To Val(.Value)
ActiveCell.Offset(1, 0).Insert Shift:=xlDown
Next I
.Visible = False
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ComboBox1.Visible = False
ComboBox2.Visible = False
End Sub
In the macro window (Alt-F8), assign the macro "InsertRow" the shortcut key Ctrl+z
In the macro window (Alt-F8), assign the macro "InsertCells" the shortcut key Ctrl+Shift+Z
To use the code, select the cell where the rows or cells will be placed under. press the appropriate shortcut, select the number of rows/cells you want to add. the combobox disappears and the cells/rows are added. if you press the shortcut then change your mind and don't want to add any cells/rows, simply click another cell.
HTH,
Maud
PitBoo1.png
Add Rows Or Cells VBA_Rev1.xlsm
Bookmarks