VBA to Insert Table Rows with Input

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,513
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
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Your code works fine for me, what issue are you having exactly? Do you have a cell on the EBank2 sheet activated when trying to insert?
 
Upvote 0
Thanks for giving your time @mrsh9898 . I exactly dont know where the problem lies. But when I execute this vba the Debugger shows error in red highlighted line. This could be probably that my worksheet has many tables. As of active cells in table, I tried it that way only.

Please help
b7GFDXaANYYBYHog9


NifUNWjSUWvenMGv6


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
Your code works fine for me, what issue are you having exactly? Do you have a cell on the EBank2 sheet activated when trying to insert?
 
Last edited:
Upvote 0
Yes. It allows that way. I just thought to make my daily exercise by working with macro...

I started with recording a macro and thought will improvise it by adding an input box. But the code I got, as a novice, didn't find a way out.

Though I have a layman's idea that if nothing else work, we can add a loop to add number of rows to the basic code until the rows to be added become zero. I t will work slow. Moreover I have idea and not knowledge to write such code.

Please help.

Thanks
What happens when you select the cell and add a row manually? Will it allow it?
 
Upvote 0
@mrshl9898 The recorded macro looks like this.

Code:
Sub AddRows()
'
' AddRows Macro
'


'
    Selection.ListObject.ListRows.Add (4)
    Selection.ListObject.ListRows.Add (4)
    Selection.ListObject.ListRows.Add (4)
    Selection.ListObject.ListRows.Add (4)
End Sub


Yes. It allows that way. I just thought to make my daily exercise by working with macro...

I started with recording a macro and thought will improvise it by adding an input box. But the code I got, as a novice, didn't find a way out.

Though I have a layman's idea that if nothing else work, we can add a loop to add number of rows to the basic code until the rows to be added become zero. I t will work slow. Moreover I have idea and not knowledge to write such code.

Please help.

Thanks
 
Upvote 0
If it allows it manually there's nothing wrong with the code that would be stopping it happening, I understand you are wanting a macro to do it, this was just a check to look for what could be causing the error.

Try this, would be good to know what the range is.

Code:
Sub InsertRows()

Dim myrange As Range
Dim x As Integer


Set myrange = ActiveCell


Debug.Print myrange.ADDRESS


    x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
    Range(myrange, myrange.Offset(x - 1, 0)).EntireRow.Insert Shift:=xlDown
    
End Sub
 
Upvote 0
Here's a plan B

Code:
Sub InsertRows()

Dim myrow As Integer
Dim x As Integer


myrow = ActiveCell.Row


    x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)


Rows(myrow).EntireRow.Resize(x - 1).Insert Shift:=xlDown
    
End Sub
 
Upvote 0
Try this:
Code:
Sub ResizeTable()
'Modified  9/10/2018  5:11:22 PM  EDT
Dim ans As Long
Dim rng As Range
Dim tbl As ListObject
ans = InputBox("Add How many rows?")
  
    Set tbl = ActiveSheet.ListObjects("EBank2")
    Set rng = Range("EBank2[#All]").Resize(tbl.Range.Rows.Count + ans, tbl.Range.Columns.Count)
    tbl.Resize rng
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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