VBA Express Forum  




Go Back   VBA Express Forum > VBA Code & Other Help > Excel Help
     Feedback     
Register FAQ Members Arcade Knowledge Base Training Articles Consulting

Reply
 
Thread Tools Display Modes
Old 04-30-2012, 10:04 AM   #1
Pendrant12

 
Joined: Apr 2012
Posts: 2
Kb Entries: 0
Articles: 0
Insert New Rows Between Rows

Hey guys,
I found out that selecting multiple rows by hand and then inserting rows would result in inserting new rows between the selected rows.
The following macro shows this:

VBA:
Sub Macro1() Range("1:1,2:2,3:3").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove End Sub
VBA tags courtesy of www.thecodenet.com

Now, what I need to do is this:

VBA:
Sub test() 'Getting the last Row Cells(1, 1).Select lastRow = Selection.End(xlDown).Row 'Building the Selection For i = 1 To lastRow selectionString = selectionString & i & ":" & i & "," Next i 'Removing the last "," selectionString = Mid(selectionString, 1, Len(selectionString) - 1) Range(selectionString).Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove End Sub
VBA tags courtesy of www.thecodenet.com
This is trying to select each row individually to insert rows between the selected rows. As expected, I get the following error as it tries to select the rows:

Run-time error '1004':

Method 'Range' of object 'Global_' failed

I know there are plenty of ways to achieve the same result by creating a simple for-loop and inserting a new row after each row.
This is NOT what I want to do, since this seems to be a lot slower. (I am speaking about 200.000-300.000 rows)
Any ideas no how to get this working without a loop?

Local Time: 11:25 AM
Local Date: 05-22-2013
Location:

 
Reply With Quote Top
Old 04-30-2012, 12:05 PM   #2
dazwm

 
Joined: Feb 2012
Posts: 30
Kb Entries: 1
Articles: 0
See if this is any good for you. You highlight the rows you want to copy and insert then enter how many times.

VBA:
Sub CopyAndInsertRow() Dim NextRow As Long Dim NrOfCopies As Long Dim i As Long Const NrOfCopiesDefault = 1 Const NrOfCopiesMaximum = 9 Do On Error Resume Next NrOfCopies = Application.InputBox(prompt:="How Many Copies Do You Want To Copy & Insert?", _ Title:="# COPIES", Default:=NrOfCopiesDefault, Type:=1) On Error Goto 0 If NrOfCopies = 0 Then MsgBox "No copies made.", vbInformation, "CANCELLED" Exit Sub ElseIf NrOfCopies > NrOfCopiesMaximum Then MsgBox "Please Enter Number Of Copies Between 1 and " & NrOfCopiesMaximum, 48, "ERROR" End If Loop While NrOfCopies < 1 Or NrOfCopies > NrOfCopiesMaximum With Selection NextRow = .Row + .Rows.Count Rows(NextRow & ":" & NextRow + .Rows.Count * (NrOfCopies) - 1).Insert Shift:=xlDown .EntireRow.Copy Rows(NextRow & ":" & NextRow + .Rows.Count * (NrOfCopies) - 1) .Resize(.Rows.Count * (NrOfCopies + 1)).Sort Key1:=.Cells(1, 1) End With End Sub
VBA tags courtesy of www.thecodenet.com


------------------------------------------------

Thanks For All Your Help

Windows 7

Excel 2010

Any codes I provide please try on a copy of your workbook first as these cannot be undone!

To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

Local Time: 08:25 PM
Local Date: 05-22-2013
Location:

 
Reply With Quote Top
Old 04-30-2012, 12:44 PM   #3
Pendrant12

 
Joined: Apr 2012
Posts: 2
Kb Entries: 0
Articles: 0
Pretty neat solution. I'll give it a try
Thanks

Local Time: 11:25 AM
Local Date: 05-22-2013
Location:

 
Reply With Quote Top
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 12:25 PM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright © 2004 - 2012 VBA Express