VBA insert rows excel macro helps while automating and dealing with the records. For example, we may automate certain task based on the number of items in certain category. And the number of items may not be equal in all the situations it may vary time to time. We will see a practical example in this topic.
- Inserting Rows in Worksheet using Excel VBA – An Example
- Inserting Rows in Worksheet using Excel VBA – Case study
- Inserting Rows in Worksheet using Excel VBA – Download Example Files
How to Insert Rows in Excel Worksheet using VBA – Solution(s):
We can insert use EntireRow.Insert method to insert rows. The following is the example code to inserting rows in excel worksheet.
VBA insert rows excel – An Example
The following example will show you how to insert a row in Excel Worksheet. You can insert multiple rows at a time.
Code:
Sub sbInsertingRows() 'Inserting a Row at at Row 2 Range("A2").EntireRow.Insert ' 'Inserting 3 Rows from 3 Rows("3:5").EntireRow.Insert End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a Module for Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to run it
Input:
Shcreen-shot of example, before executing of the above code. You can see the 10 rows of data available in the worksheet.
Output:
Shcreen-shot of example, after executing of the above code. You can see the 4 new rows are inserted in the worksheet.
Inserting Rows in Worksheet using Excel VBA – Case study
The following example create the list if items by inserting the rows based on the numbers mentioned for each category.
Code:
Sub sbInsertingRowsCaseStudy() Dim iCntr, jCntr For iCntr = 2 To 4 ' for each category 'Find the start row of category startRow = Application.WorksheetFunction.Match(Cells(iCntr, 1), Range("A16:A3300"), 0) + 15 'assuming maximum items are around3000 For jCntr = 1 To Cells(iCntr, 2) 'print items Rows(startRow + 2).EntireRow.Insert Cells(startRow + 2, 2) = "Item " & Cells(iCntr, 2) - jCntr + 1 Next Next End Sub
Instructions:
Download the example file and click on the ‘Create Category List’, it will create the categories based on the number mentioned for each category.
Inserting Rows in Worksheet using Excel VBA – Download: Example File
You can download the example file and see example codes on Inserting Rows in Excel Worksheet.
I need small information .In excel every 27 rows after insert 5 rows.how to do this .Please suggest to me
Hi Venkat,
Here the VBA macro to insert n umber of rows after every nth row:
Hope this helps!
Thanks-PNRao!
I want to create a column before some specific text inside the cell .can you please help me in this ?
Hi Spandan,
You can loop through the columns and check if the condition (required text matches), then add insert the column.
check this link:
http://analysistabs.com/excel-vba/inserting-columns-worksheet/
Thanks-PNRao!
Hi, I would like to create a number of rows based on a cell input.
For example, based on input = 3 in a certain cell, I want to create 3-minus-1 rows.
How do I do this? Many thanks!
You can write something like this, lets say you are entering the number at Range A1 and you want to insert the new rows starting from Row2:
Hi,
Wondering if this can be modified to suit something I’ve been struggling with:
The code listed below is tasked to :
> Create a new sheet based on each page break (which have been inserted through the subtotals function)
> Save the sheet to a designated location and,
> Auto-name each sheet according to the value in cell A2.
Code below:
“Sub Sample()
Dim rowCurrent As Long, rowPrevious As Long, i As Long
Dim oWB As Workbook, newWbk As Workbook
Dim oWS As Worksheet
Set oWB = ActiveWorkbook
Set oWS = oWB.Sheets(“Specials”)
rowPrevious = oWS.UsedRange.Row + oWS.UsedRange.Rows.Count – 1
For i = oWS.HPageBreaks.Count To 0 Step -1
If i = 0 Then
oWS.Rows(“1:” & rowPrevious).Copy
Else
rowCurrent = oWS.HPageBreaks(i).Location.Row
oWS.Rows(rowCurrent & “:” & rowPrevious).Copy
End If
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs “file_path ” & ActiveSheet.Range(“A2″).Value & -i
ActiveWorkbook.Close
rowPrevious = rowCurrent – 1
Next
End Sub”
What I want to do, is take the column headings from the main file and insert them as row 1 on each sheet.
Would greatlly appreciate the assist.
David
Good morning
I am trying to insert 9 blank rows after every line of text and am a loss as to how to write the script. I have had a look at the example above and am wondering what the script should look like?
Thank you in advance
Rob
I am trying to add an add row button to several sections in Excel. I can execute the commands and get the rows to add but when it runs if you add a row to section 1 at the end of the existing rows it works fine, but in section two the new row is added within the section and not at the end of the section. How do I write the script to always add a row at the end of the section regardless of the new rows added above the section?
I have been looking for a way to add rows to a worksheet based on the number in a cell. This is the only place (of many) I found something that works. Thanks!
Mike
must have to set object variable
Hi Erin
I’m struggling with this one too. It does work if you click on one of the rows within the range of data before you run the macro though.
Hi Simon,
Could you please explain your requirement and share the code which you have tried.
Thanks-PNRao
Can you share the code please. I am unable to use the above one.
Hi Ash,
Please click on Download Now to download the example file and see the example codes on Inserting Rows in Excel Worksheet at the end of the page.
Regards-PNRAO
Hi!
I find very useful all your informations!
I have a question: I have a workbook and I want to add an intire row that contains data or functions, directly beneath. The “problem” is that there are other rows that I want them to move down. For example, if I want to copy row A1, there are data in row A2,AE etc and I want them to move one row down so I can stell use them..
Thanks in advance for your reply
Hi I wants to add row if data/value found, else not.
so, there are many line items but not very specific sequence.
please provide macro.
Thank you
Hi, How can I copy the formulas in a row to the next row.
Hi,
I want a macro for rows that are having phone numbers more than 1 e.g.,
from
xxxxxx 234555, 455555, 5677777, 567778
xxxxxxxx 455656
fdgggfdf 7878787, 455550
xxxxxxxx 455656
to
xxxxxx 234555
xxxxxx 455555
e.g., 234555, 455555, 5677777, 567778 these numbers should come one after the other by using macro code.
These can occur randomly on my data sheet, it can be 2, 3, 4, or 5 numbers. But they will have the same name in separate colm.
I want them to be inserted as new cells, so that they do not overlap the numbers below
Hi, What if I have a row of heading and want to skip it, the above macro helps when there is no heading but if I have a heading it will go for a toss.
Hi , What I mean is how to tell the macro to skip first n rows so header wont be affected and then run the macro like above!
Hi I need a macro which will insert multiple rows below consecutive rows and insert particular data in the newly insrted rows.
Example :-
33498 000001 ABC
33498 000001 PQR
33498 000001 MNB
33498 000001
33498 000001 ASD
33498 000001 AZXC
33498 000001
33498 000001 AWE
33498 000001
33498 000001 QWE
33498 000001 CDD
33689
33456
Here it whould add 11 rows for 33689 and copy the data from ABC to CDD from column 3(including blank cells). Below is the output expected
33498 000001 ABC
33498 000001 PQR
33498 000001 MNB
33498 000001
33498 000001 ASD
33498 000001 AZXC
33498 000001
33498 000001 AWE
33498 000001
33498 000001 QWE
33498 000001 CDD
33689 000001 ABC
33689 000001 PQR
33689 000001 MNB
33689 000001
33689 000001 ASD
33689 000001 AZXC
33689 000001
33689 000001 AWE
33689 000001
33689 000001 QWE
33689 000001 CDD
33456 000001 ABC
33456 000001 PQR
33456 000001 MNB
33456 000001
33456 000001 ASD
33456 000001 AZXC
33456 000001
33456 000001 AWE
33456 000001
33456 000001 QWE
33456 000001 CDD
Hi,
Good day!
I would like to ask help on how to create a macro on my data sheet.
I have a list of company names in column A and then I need to insert 50 rows after each name because I going to insert 51 state jurisdiction in column E to be able to search each name in each state.
Column A (ENTITY NAMES) B C D Column E (State search)
Microsemi Storage Solutions, Inc. AK
Microsemi Storage Solutions, Inc. AL
…..
Microsemi Storage Solutions, Inc. WY
PMC-Sierra US, Inc. AK
PMC-Sierra US, Inc. AL
PMC-Sierra US, Inc. AR
….
Wintegra, Inc.
Thank you so much..
I have following table
header1
row1
row2
row3
need to convert as below
header1
row1
header1
row2
header1
row3
Can anyone share code for this….note the row count is changing every time you insert
Thanks.
Hi PNRao,
I want to add Row on specified sheet. number of row to inserted is mention in Cell ( for example: E10) of instruction sheet. when i run macro than this macro should enter number of row mention in cell E10 in sheet name XYZ.
I have two worksheet. i will make separate macro for each sheet to do same thing. as both sheet have different figure and 1st i need to insert on one sheet after getting other data than i need to add raw sheet to other sheet.
The below VBA code will, insert the number of rows specified:
Thanks-PNRao!
I have a spreadsheet where I add a row every day and add new information into that row manually. There are also some formulas that autofill when the new line is inserted. I have a total at the very bottom of the spreadsheet. How do you change this VBA code to insert a line just above the total as opposed to the number it is at now “301”? The way the code is now, it always inserts a line at 301, but I want it to insert right above the total line regardless of which line it is on. I have also created other VBA formulas in the totals row which sum, count, and then a combined formula to get the average using the sum and count (all based on colors of cells). Thanks in advance for your help!
Sub Inserting_Line()
‘
‘ Inserting_Line Macro
‘
‘
Rows(“301:301”).Select
ActiveSheet.Unprotect
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range(“A299:Y299”).Select
Selection.AutoFill Destination:=Range(“A299:Y300”), Type:=xlFillDefault
Range(“A299:Y300”).Select
End Sub
I want to insert 8 rows after each 20 row. It’s my first time using VBA in excel.
hello PNRao,
what VBA code can i put if i want to copy down data validation rule to next cell based on the condtion that previous cell is empty or not?
Hi,
I am new to VBA coding and writing macros. i am trying to insert a new row based on my cursor position. This is the code which i am tried using but it gives error like Run-time error “1004”: method ‘Range’ of object ‘_Global’ failed. please suggest how to corect it.
Sub insertrow()
‘Inserting a new row at my cursor postion
Range(“xlapp.ActiveCell.Row”).EntireRow.Insert
End Sub
hi, i have got solution to my previous problem and now i am bale to insert row and column at my desireed location(here cursor location). now i have one more problem which is i am not able to undo the cells created. i want to create a code so that it undo’s or brings excel to original sheet once i open it again.
Hi,
I have a column that has numbers like 30, 60 ,90,120 and so on.I want a macro for inserting a row when there is difference of 120 or above. can you please help.
Here is the code to insert a row if there difference is more than a certain value:
Thanks!
Hi Reva,
What’s the new code? I am looking for the solution too.
Thanks,
JUnn
Hi,
I’m struggling with trying to copy a row from one file and insert the row in another file. Please help
Range” file name ABC”(“B1:F1″).Copy
Range” File name XYZ”(“A1″).EntireRow.Insert
Range” File name XYZ”(“B1:F1”).Select
ActiveSheet.Paste ‘
Application.CutCopyMode = False
End
File ABC has only one worksheet, whereas file XYZ has 56 worksheets, that is to say that range B1:F1 go to worksheet 1, range B2:F2 go to worksheet 2 and so on. Thanks
Hello,
Here is the VBA code to copy to insert new row and paste the copied data in workbook.
You can use a for loop and change the macro accordingly. Here is the VBA code to insert rows in each sheet of the workbook and copy the data and paste it in newly created row.
I’m making a model for startup costs, construction time and profit. I’m trying to create a macro that takes a varying value input (construction time) and generates rows based on the input. The cells in the generated rows need to contain a constant divided between them equally (the startup cost). So the construction time may vary and I can’t seem to get this right.
I am wondering if it is possible to use the information from row 1 A1 and row 2 A1 to create a different number of rows between the 2.
what i am thinking is taking row 1 A1 information minus row 2 A1 information dived by 60 and that is the number of lines that need to be created between row A and B. And them repeat this all the way thru the document. between 2-3 thousand lines. to create a document with around 120 thousand lines.
HI, I have a macro that inserts a new row into my table, I also have a macro that inserts a complete new table for recording absence. The trouble I have is that when inserting the new table the tables already existing get moved downwards as requested but my insert row macro for the second table no longer works as it is still referencing from the above location.
Hope this makes sence
I need to Insert with the data in it ,at the end . I need to insert in the middle
My data is currently like this: Column A = Property Name, Column B = Routines. The Routines column has multiple values.
Screenshot 1
In another sheet I have mapped the Routine data values. For Example, where the value in the ‘Routines’ column A was previously ‘Value 1 & Value 2’ there is now Column B with ‘Value 1’ and Column C with ‘Value 2’.
Screenshot 2
Now, In a new sheet I need my data to be in the following format Column A: Property Name, B: Mapped Routine, where each Routine Value is represented by a new row. For example: Row 1: Column A: Property A, Column B: Value 1 Row 2: Column A: Property A, Column B: Value 2
Screenshot 3
The only thing I can think to do is to X-Lookup the Value 1 in the New Routine format (i.e. Where ‘Value 1 – Value 2 & Value 3’ bring back ‘Value 1’) and then manually create new rows and bring back Value 2. But this isn’t doable as there can be up to 5 New Routine Values for around 400 old Routines. Does anybody have any suggestions?