VBA to Insert Table Rows with Input

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,514
Office Version
  1. 2024
Platform
  1. Windows
I have a worksheet with many tables in it. In that sheet there is a table "EBank2".

I need to
add multiple rows in this table based on the value in an input box.

Further, If it may help me add rows below a specific active cell, would be great.

I have tried many codes but nothing has worked for me.

Please help.

Thanks a lot

Rich (BB code):
Sub InsertRows()

    Dim x As Integer
    x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
    Range(ActiveCell, ActiveCell.Offset(x - 1, 0)).EntireRow.Insert Shift:=xlDown
    
End Sub
 
The following macro inserts list rows below the active cell based on the user input. It allows for multiple tables within the worksheet, whether the tables appear side by side or one above the other or both. It also includes some error handling as well.

Code:
Option Explicit

Sub InsertListRowsBelowActiveCell()


    Dim objListObject As ListObject
    Dim varNumRows As Variant
    Dim lngPosition As Long
    Dim i As Long
    
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub


    Set objListObject = ActiveCell.ListObject
    
    If objListObject Is Nothing Then
        MsgBox "Please select a cell within a table, and try again!", vbExclamation
        Exit Sub
    End If
    
    If Not Application.Intersect(objListObject.HeaderRowRange, ActiveCell) Is Nothing Then
        MsgBox "Please select a cell within a table, excluding the" & vbNewLine & _
            "header row, and try again!", vbExclamation
        Exit Sub
    End If


    Do
        varNumRows = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
        With Application
            If .IsLogical(varNumRows) Then Exit Sub
            If varNumRows > 0 Then Exit Do
        End With
        MsgBox "You must enter a number greater than 0!", vbExclamation
    Loop
    
    lngPosition = ActiveCell.Row - objListObject.DataBodyRange.Rows(1).Row + 2
    
    For i = 1 To varNumRows
        objListObject.ListRows.Add lngPosition
    Next i
    
End Sub

Hope this helps!
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
@Domenic Thanks. It did the magic for me! Thanks again.

The following macro inserts list rows below the active cell based on the user input. It allows for multiple tables within the worksheet, whether the tables appear side by side or one above the other or both. It also includes some error handling as well.

Code:
Option Explicit

Sub InsertListRowsBelowActiveCell()


    Dim objListObject As ListObject
    Dim varNumRows As Variant
    Dim lngPosition As Long
    Dim i As Long
    
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub


    Set objListObject = ActiveCell.ListObject
    
    If objListObject Is Nothing Then
        MsgBox "Please select a cell within a table, and try again!", vbExclamation
        Exit Sub
    End If
    
    If Not Application.Intersect(objListObject.HeaderRowRange, ActiveCell) Is Nothing Then
        MsgBox "Please select a cell within a table, excluding the" & vbNewLine & _
            "header row, and try again!", vbExclamation
        Exit Sub
    End If


    Do
        varNumRows = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
        With Application
            If .IsLogical(varNumRows) Then Exit Sub
            If varNumRows > 0 Then Exit Do
        End With
        MsgBox "You must enter a number greater than 0!", vbExclamation
    Loop
    
    lngPosition = ActiveCell.Row - objListObject.DataBodyRange.Rows(1).Row + 2
    
    For i = 1 To varNumRows
        objListObject.ListRows.Add lngPosition
    Next i
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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