Solved

VBA / Excel - inserting rows between each of several hundred rows

Posted on 2013-11-26
11
363 Views
Last Modified: 2013-11-26
In column A I have several hundred rows of data.  I want create 2 copies of each row and insert it under the original row.  EG I want to insert 2 copies of row 2 just below row 2, causing what is currently row 3 to move to row 5.  I then want to repeat this for what is now row 5, and repeat until all the orignal rows  have 2 copies beneath their original row,

Then in column B, against each set of idential items in column A I want to put text string 1 against the first, text string 2 against the second and text string 3 against the 3rd.  EG after completing the procedure cells B2:B4 will contain respectively text string1 - text string 3.

I am sure this can be done in VBA but I am still a total novice with VBA so would appreciate some help.

Thanks

Eric
0
Comment
Question by:childejc
  • 4
  • 4
  • 3
11 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39677284
Hi,

pls try

Sub macro()

Set myRange = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))

For Idx = myRange.Rows.Count + 1 To 2 Step -1
    Range("A" & Idx).EntireRow.Copy
    Range("A" & Idx).Insert Shift:=xlDown
    Range("A" & Idx).EntireRow.Copy
    Range("A" & Idx).Insert Shift:=xlDown
    Range("A" & Idx).Offset(, 1) = "text string 1"
    Range("A" & Idx).Offset(1, 1) = "text string 2"
    Range("A" & Idx).Offset(2, 1) = "text string 3"
Next


End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:childejc
ID: 39677288
Thanks!  You have saved me a lot of grief!
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39677306
Although the question is closed, there is a non VBA method if you are interested.
0
 

Author Comment

by:childejc
ID: 39677334
Yes please, I am interested...
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39677356
Lets say your data is in column A and you want to populate column B with textstring.

Fill column C with numbers 1 to 10000 or whatever you have.
Copy A1:C10000 twice below to A10001:C20000 and A20001:C30000
Fill B1:B10000 with textstring1
Fill B10001:B20000 with textstring2
Fill B20001:B30000 with textstring3
Sort A1:C30000 with column C as the primary key and column B as secondary key
Delete column C
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:childejc
ID: 39677414
Good idea, I can see that woudl also do the job, and probably more quickly, and I will bear it in mind for the future.

The VBA code provided earlier is still running as we speak churning through my data, and is somewhat slower than I expected.  However I am trying to learn VBA and so this was a good opportunity to learn something relevant to my actual current needs, so I did want a VBA solution.  Now I just need to try to understand it!
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39677426
I suggest that you
halt the macro,
turn off automatic calculation mode,
resume the macro,
when it ends turn on automatic calculation mode.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39677459
Hi,

I should have integrated Screenupdating false

Sub macro()

Application.ScreenUpdating = False
Set myRange = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))

For idx = myRange.Rows.Count + 1 To 2 Step -1
    Range("A" & idx).EntireRow.Copy
    Range("A" & idx).Insert Shift:=xlDown
    Range("A" & idx).EntireRow.Copy
    Range("A" & idx).Insert Shift:=xlDown
    Range("A" & idx).Offset(, 1) = "text string 1"
    Range("A" & idx).Offset(1, 1) = "text string 2"
    Range("A" & idx).Offset(2, 1) = "text string 3"
Next
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39677496
I think in this case recalculation is more critical as he has spent over an hour. Screenupdating is also good.
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39677536
HI,

adjusted code

Sub macro()

myCalc = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Set myRange = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))

For idx = myRange.Rows.Count + 1 To 2 Step -1
    Range("A" & idx).EntireRow.Copy
    Range("A" & idx).Insert Shift:=xlDown
    Range("A" & idx).EntireRow.Copy
    Range("A" & idx).Insert Shift:=xlDown
    Range("A" & idx).Offset(, 1) = "text string 1"
    Range("A" & idx).Offset(1, 1) = "text string 2"
    Range("A" & idx).Offset(2, 1) = "text string 3"
Next
Application.Calculation = myCalc
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
0
 

Author Comment

by:childejc
ID: 39677690
Thanks for this - I am new to this site and am very impressed by the quality of responses and helpfulness of contributors.

It took about 1.5 hours in the end (around 500 rows with 8 duplications for each row), and I did not see this response until it was done.  However I need to repeat the process for another sheet with around 400 rows, so will use this modified code (with manual calculation) for that to see how it improves matters.

Thanks again

Eric
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now