![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: England
Posts: 212
|
Can someone help me with some code that will insert 2 rows between a used range, e.g. if I have data in rows 2-100, how do i insert 2 rows between each of these rows?
Thanks Matt |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 10,387
|
Here's code that will do that, and it's versatile because it involves an input box, so if you change your mind regarding how many rows to insert, just enter that number in the box.
Select the first cell in the range where you want to insert the rows, and run this macro. Modify for column. ''''''''''''''''''''''''' Sub AddRows() Dim InsQuan As Integer On Error Resume Next InsQuan = InputBox("Enter number of rows to insert:", "Your Call") If InsQuan <= 0 Then MsgBox "Invalid number entered", 16, "Action cancelled." Exit Sub End If Application.ScreenUpdating = False Do Until Selection.Value = "" ActiveCell.Offset(1, 0).Range("A1:A" & InsQuan).Select Selection.Insert Shift:=xlDown ActiveCell.Offset(InsQuan, 0).Select Loop [A1].Select Application.ScreenUpdating = True End Sub '''''''''''''''''''''''' Thanks to Mark W, who previously posted this non-VBA approach (though you asked for code in your question, this way is effective as well): 1. Number your rows (1 to 300 or whatever the last row is) in an unused column. 2. Paste a copy of these numbers (1 to 300 or whatever) directly beneath the copied numbers -- on unused rows. Do it again, because you want to insert 2 rows. 3. Sort (Ascending) on the column that contains the numbers, then clear the contents (the numbers) of that column. |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: England
Posts: 212
|
Thanks Tom
|
|
|
|
|
|
#4 | |
|
Join Date: Mar 2002
Posts: 22
|
Quote:
Also, a macro could be recorded by doing it this way, which would have the advantage of not needing any knowledge of VBA and perhaps having a faster run time. |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|