Insert Row using Range objective

Vandana Jain

New Member
Joined
Nov 14, 2015
Messages
3
Greetings there,

I have been trying to write code for a problem.
Problem is:

I need to insert some rows at 4 places in a worksheet at a time but at different ranges in same number.
Like, I need to insert 4 rows at Range 12, 24, 30, 45.
Every time, I will add rows at 4 places for sure but ranges would be different.

I have written some code for this:



Dim J as Long, Rng as String, I as Integer, K as Integer
J = InputBox("Type the number of paths to be inserted")
For K = 1 To 4
Rng = InputBox("Enter your range")
For I = 1 To J
Range(Rng).EntireRow.insert
Next
Next




Now, this block of code shows some error with Range Objective.
Please help me out.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try...

Code:
Sub insertRws()
    Dim vSplit, strName As String, i As Long, j As Long
    
    j = InputBox("Type the number of paths to be inserted")

    strName = InputBox(Prompt:="Input values separated by comma", _
                       Title:="Insert row position", Default:="Input values separated by comma i.e. 5,9,12")

    vSplit = Split(strName, ",")
    For i = LBound(vSplit) To UBound(vSplit)

        Rows(vSplit(i)).Resize(j).Insert
    Next i

End Sub
 
Last edited:
Upvote 0
or

Code:
Sub insertRws()
    Dim vSplit, strName As String, i As Long, j As Long
    
    j = InputBox("Type the number of paths to be inserted")

    strName = InputBox(Prompt:="Input values separated by comma", _
                       Title:="Insert row position", Default:="Input values separated by comma i.e. 5,9,12")

    vSplit = Split(strName, ",")
    For i = j To LBound(vSplit) Step -1

        Rows(vSplit(i)).Resize(j).Insert
    Next i

End Sub

depending on exactly what your interpretation of where to input the rows is.
Row numbers must be entered in ascending order
 
Last edited:
Upvote 0
Going crazy on the last code try this one instead (and the one in post #2)

Rich (BB code):
Sub insertRws()
    Dim vSplit, strName As String, i As Long, j As Long
    
    j = InputBox("Type the number of paths to be inserted")

    strName = InputBox(Prompt:="Input values separated by comma", _
                       Title:="Insert row position", Default:="Input values separated by comma i.e. 5,9,12")

    vSplit = Split(strName, ",")
    For i = UBound(vSplit) To LBound(vSplit) Step -1

        Rows(vSplit(i)).Resize(j).Insert
    Next i

End Sub

Row numbers still must be entered in ascending order
 
Upvote 0
Shows run time error 1004:
Application defined or object defined error

at line:
Rows(vSplit(i)).Resize(j).Insert


Help me out
 
Upvote 0
Put a single number in the first input box say a 4.
Put a series of numbers in ascending order separated by a comma in the 2nd inputbox i.e. 6,15,19.

and yes both the codes in post #2 and #4 work for me without error.

They do both give different results (and are supposed to).

You will get a 1004 error if the sheet is protected and you haven't allowed rows to be inserted.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,111
Members
452,302
Latest member
TaMere

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