Search : in
By :

Copy row and insert n times

Last answer on Oct 29, 2009 11:06:24 pm GMT Islandman, on Jun 12, 2009 4:17:49 pm BST 
 Report this message to moderators

Hello,
I have an example below, I'd like to take the first row, copy it "n" number of times, then take the next row copy it the same "n" number of times, till the end of the rows.

Sample
ColA ColB ColC
TextA TextA1 TextA2
TextB TextB1 TextB2
TextC TextC1 TextC2

Results (for example 3 times)
ColA ColB ColC
TextA TextA1 TextA2
TextA TextA1 TextA2
TextA TextA1 TextA2
TextB TextB1 TextB2
TextB TextB1 TextB2
TextB TextB1 TextB2
TextC TextC1 TextC2
TextC TextC1 TextC2
TextC TextC1 TextC2

Configuration: Windows XP Internet Explorer 8.0

Best answers for « Copy row and insert n times » in :
Inserting space between each letter ShowInserting space between each letter $ echo -e "How are you\n everything alright" | sed 's/./& /g' H o w a r e y o u E v e r y t h i n g a l r I g h t The disadvantage is that the natural space between each word...
Inserting text in a file ShowInserting text in a file Introduction Syntax Introduction It is sometimes useful to insert text in the header of one or more document (s) or in a specific part of the document (before or after a specific line). The publisher of...
Inserting an image in gmail when composing a message ShowInserting an image in gmail when composing a message Here is a great way to insert images in your Gmail e-mails. Guaranteed results! Since you have a Gmail account (since you ask this question) you need access to Google Docs. This...
Download CopyTrans Suite ShowCopyTrans Suite is a set of practical tools for your Ipod or Iphone. It contains: · CopyTrans v2.37 · CopyTrans Photo v2.21 · iLibs v1.13 · iCloner v1.07 · CopyTrans Doctor v1.015 · CopyTrans Manager v0.720 · SyncGuardian...
PC assembly - Inserting expansion cards ShowInserting expansion cards Expansion cards are plugged into expansion slots. There are several types of slots: ISA slots run at 16 bits. Few computers still use this type of slot as the bus runs at a relatively slow speed. PCI slots run at 32...
PC assembly - Inserting memory modules ShowInserting RAM modules There are various types of random access memory. The latest motherboards are equipped with DDR2 or DDR memory. Some are even equipped with RAMBus memory. The oldest ones actually have SDRAM connectors, or even EDO RAM...

venkat1926, on Jun 13, 2009 7:08:50 am BST

Try this macro

Sub test()
Dim n As Integer, rng As Range
n = InputBox("type the value of n")
Set rng = Range("a1")
rng.Select
line2:
Range(rng.Offset(1, 0), rng.Offset(3, 0)).EntireRow.Insert
Range(rng, rng.End(xlToRight)).Copy
Range(rng, rng.Offset(n, 0)).PasteSpecial
Set rng = rng.Offset(n + 1, 0)
If rng = "" Then
GoTo line1
Else
GoTo line2
End If
line1:
Application.CutCopyMode = False
Range("a1").Select
MsgBox "macro over"
End Sub

Reply to venkat1926

2

islandman63, on Jun 15, 2009 9:55:34 pm BST

Venkat,

it works great, but only for the first row, after that, it doesn't pick up the second row, and copy down n times. Is there a way to loop through until all originals rows are copied/pasted n times?

I love the way VB works, i just wish i could program using it. Thank you so much for all your help.

Reply to islandman63

3

venkat1926, on Jun 16, 2009 1:12:29 am BST

It works in my case. do an experiment open a new workbook.
type a in a1 and s in A2 and d in a3 and f in a4. now copy paste my macro in this workbook's vb editor and then run the macro . in the inptbox type for example 3 . Do you not get what you want . that is a repated 3 tiems, s repeated 3 times etc.



Then you can find out what is the problem in your workbook

a
s
d
f

Reply to venkat1926

5

NoraRoberts, on Sep 3, 2009 2:59:08 pm BST

Hi Venkat,

The macro works great to duplicate each row with the same nth times but is there anyway I can copy each row and paste different "nth" times.

For example.

I'd like date in A1 to be pasted 5 times.. whereas A2 to be pasted 3 times depending on the number I input.

I'd appreciate if you could help with this issue,

- Raj

Reply to NoraRoberts

4

islandman63, on Jun 16, 2009 1:46:11 am BST

Venkat,

Your absolutely right it works on the example great. How do i attach a file so i can use the real file to tweak?

Reply to islandman63

6

venkat1926, on Sep 4, 2009 1:08:57 am BST

1.open your file
2. open vb editor (alt+F11)
3.control+R
4.in the left side window highlight your file name
5. click insert(in the menu bar of vb editor)-module
6.there you copy paste the macro
7. save teh file
8. now you run the macro( I am sure you know how to do it)

any doubts post back

Reply to venkat1926

7

NoraRoberts, on Sep 4, 2009 12:02:46 pm BST

Hello Venkat,

I think you mistook my post as someone elses. Here's my questions again,

------
The macro works great to duplicate each row with the same nth times but is there anyway I can copy each row and paste different "nth" times.

For example.

I'd like date in A1 to be pasted 5 times.. whereas A2 to be pasted 3 times depending on the number I input.

------

I'd appreciate if you could help with this issue,

- Raj

Reply to NoraRoberts

21

ja, on Sep 30, 2009 12:27:28 pm BST

Most impressive, I am NOT a programmer, but I do know excel (not VBA)...

My issue, if you can assist is very similar ... I have data like this:
Col A Col B
100 12,13,14
101 23,24

Which needs to look like this

Col A Col B
100 12
100 13
100 14
101 23
101 24

If you could assist with one of your great macros ... I would be very grateful!!

Reply to ja

8

venkat1926, on Sep 4, 2009 12:39:07 pm BST

Try this

Sub test()
Dim n As Integer, rng As Range
'n = InputBox("type the value of n")
Set rng = Range("a1")
rng.Select
line2:
n = InputBox("type no. of times you want to be repeated minus 1 for e.g if you wnat to be repeated 3 times type 2")
Range(rng.Offset(1, 0), rng.Offset(n, 0)).EntireRow.Insert
Range(rng, rng.End(xlToRight)).Copy
Range(rng, rng.Offset(n, 0)).PasteSpecial
Set rng = rng.Offset(n + 1, 0)
If rng = "" Then
GoTo line1
Else
GoTo line2
End If
line1:
Application.CutCopyMode = False
Range("a1").Select
MsgBox "macro over"

End Sub

Reply to venkat1926

9

Netscur, on Sep 8, 2009 6:57:50 pm BST

I have a problem that might be similar to the history in this forum entry.


I create a forecast for parts in a cross tabular format (example below)
ColA ColB ColC ColD ColE

Part 1 Month1 Month2 Month3 Month4
Part 2 Month1 Month2 Month3 Month4
Part 2 Month1 Month2 Month3 Month4



To load the forecast into my Enterprise software program, I need the data to be tabular:
ColA ColB

Part 1 Month1
Part 1 Month2
Part 1 Month3
Part 1 Month4
Part 2 Month1
Part 2 Month2
Part 2 Month3
Part 2 Month4
Part 3 Month1
Part 3 Month2
Part 3 Month3
Part 3 Month4
Part 4 Month1
Part 4 Month2
Part 4 Month3
Part 4 Month4


The number of "parts" varies month to month; I would like to define it when the macro first runs. In my example, there were 4 parts.

Most of the time I create a forecast for '12 months' but it would be more useful to also define the date range when the macro runs. In my example, there were just 4 months.


Finally, I would prefer that the macro paste the data in a 'new worksheet' (rather than over write my forecast data worksheet).



Any help would be of immense use. Thank you!

Reply to Netscur

10

venkat1926, on Sep 9, 2009 2:23:56 am BST
  • +1

Netscyr

this is the macro

Sub test()
Dim rng As Range, c As Range
Dim rng1 As Range, c1 As Range
Dim dest As Range, j As Integer, k As Integer
Worksheets("sheet2").Cells.Clear
With Worksheets("sheet1")
Set rng = Range(.Range("A2"), .Range("A2").End(xlDown))
j = WorksheetFunction.CountA(.Rows("1:1"))
'msgbox j
For Each c In rng
Set rng1 = Range(c.Offset(0, 1), .Cells(c.Row, Columns.Count).End(xlToLeft))
'msgbox rng1.Address
For Each c1 In rng1

Set dest = Worksheets("sheet2").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)
'msgbox dest.Address
If c1 = "" Then GoTo line1
dest.Offset(0, 0) = c
dest.Offset(0, 1) = .Cells(1, c1.Column)
'dest.Offset(0, 2) = c1
line1:
Next c1

Next c
End With
End Sub


another method -pivot table of database.-
ref: wlakenbach blog
http://spreadsheetpage.com/...

Reply to venkat1926

11

Netscur, on Sep 9, 2009 3:56:17 pm BST

Aces... with minor tweaking for my 'real' document, it WORKS great, and I understand 50% of how it works :)


There is one last ‘need’ for my Enterprise upload... the DATE found in and across “Row 1, ‘$A$2:$A*’”

I need the macro to take the Rawdata, which is the forecast in cross tabular form (see below)
Rawdata:
ColA ColB ColC ColD

Item 6/1/2009 7/1/2009 8/1/2009
Part 1 50 55 6
Part 2 1 5 10



and produce the following results:
sheet2:
ColA ColB ColC

Part 1 39965 6/1/2009
Part 1 39995 7/1/2009
Part 1 40026 8/1/2009
Part 2 39965 6/1/2009
Part 2 39995 7/1/2009
Part 2 40026 8/1/2009


With what you provided me (THANK YOU SO MUCH) I am able to create ColA & ColB.
If I need to, I am able to copy & paste special ("transpose") the date range from the Rawdata. Unfortunately, when I have hundreds of parts it is just a little time consuming… so if the action can be incorporated into the macro, it would make my job go even faster.

Thank you again. Just so you don't think I am a leach wasting your talent, I have already signed up for Tutorial Excel & Access lessons with our Microsoft IS/IT package. Being able to do what you created will definitely make me more valuable.

Reply to Netscur

12

Netscur, on Sep 9, 2009 6:42:00 pm BST

I apologize, I miss typed in my sample/request above....



I need the additional macro lines to take the Rawdata, which is the forecast in cross tabular form (see below)
Rawdata:
ColA ColB ColC ColD

Item 6/1/2009 7/1/2009 8/1/2009
Part 1 50 55 6
Part 2 1 5 10

("Item", "6/1/2009", "7/1/2009, "8/1/2009" are headers)


and produce the following results:
sheet2:
ColA ColB ColC

Part 1 50 6/1/2009
Part 1 55 7/1/2009
Part 1 6 8/1/2009
Part 2 1 6/1/2009
Part 2 5 7/1/2009
Part 2 10 8/1/2009



As my lack of knowledge regarding macros is abundant, perhaps this is an entirely different macro than the first you provided. No matter what, I appreciate your guidence and effort!

Reply to Netscur

13

venkat1926, on Sep 10, 2009 3:38:49 am BST
  • +2

It is a bother to modify a already created macro even it it is one's own.
I have given you another macro similar to one given before.

Sub test()

Dim rng As Range, c As Range
Dim rng1 As Range, c1 As Range
Dim dest As Range, j As Integer, k As Integer
Worksheets("sheet2").Cells.Clear
With Worksheets("sheet1")
Set rng = Range(.Range("A2"), .Range("A2").End(xlDown))
j = WorksheetFunction.CountA(.Rows("1:1"))
'msgbox j
For Each c In rng
Set rng1 = Range(c.Offset(0, 1), .Cells(c.Row, Columns.Count).End(xlToLeft))
'msgbox rng1.Address
For Each c1 In rng1

Set dest = Worksheets("sheet2").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)
'msgbox dest.Address
If c1 = "" Then GoTo line1
'dest.Offset(0, 0) = c
'dest.Offset(0, 1) = .Cells(1, c1.Column)
'dest.Offset(0, 2) = c1
dest = c
dest.Offset(0, 1) = c1
dest.Offset(0, 2) = .Cells(1, c1.Column)
line1:
Next c1

Next c
End With
With Worksheets("sheet2").Columns("c:c")
.NumberFormat = "dd-mmm-yy"
End With
End Sub

Reply to venkat1926

14

Netscur, on Sep 10, 2009 7:59:08 pm BST

WOW, everything I needed!!! Thank you kindly!

Just out of curiosity, are you self taught or have you taking some training to write in Visual Basic so fluently?
Netscur (Minneapolis, MN)

Reply to Netscur

15

venkat1926, on Sep 11, 2009 5:08:36 am BST

I am only self taught. that is why I am not an expert. I suggest to learn vba just RECORD macro following the steps you want take and edit the macro This is the best way to learn vba. any doubt go to google search. and also take help of forums.

Reply to venkat1926

16

alokk, on Sep 22, 2009 6:41:06 pm BST

Hi Venkat,

Just following your thread there about copying and inserting rows as per a condition. Sorry to innundate you with similar questions, but I have a similar problem to solve but with a slight twist and that is

I need the macro to work out the interval between two dates (start & End date) which I input and then accordingly duplicate the record that many times so lets

the number of days between 23/09 and 25/09 is 3 (inclusive of 25th) the I need the macro to first copy and insert three rows of the same data lets say which is on row 16, except that the start date on each of those 3 rows changes until it is equal to the end date.

It's a bit of twister for me as a complete newbie, I have been getting along all this while recording macros, but this one will not take a mouse click for an answer. Any help in this regard will be greatly appreciated.

Thanks in advance
Alok

Reply to alokk

17

venkat1926, on Sep 23, 2009 2:23:11 am BST

1. how is row no. 16 configure . does this row contain the date or only with out date
2. how are you going to input start and end date
give an example of row no 13 if necessary with fictitious data

Reply to venkat1926

18

alokk, on Sep 23, 2009 3:52:29 am BST

Yes row 16 will contain both start and end dates.
The input for both is coming through a combo box in a form

This will be the 1st row of records I keep inserting rows to add more data.

Start Date End Date Start Time End Time Group Sub Group Details Manager
23-09-09 25-09-09 10:00 AM 11:00 AM Class X section 1 Goes for preparation Teacher 1


So in this case when a button is clicked I would need the final rows to look something like this

Start Date  End Date  Start Time  End Time  Group  Sub Group  Details  Manager
23-09-09  25-09-09  10:00 AM  11:00 AM  Class X   section A  preparation  Teacher 1
24-09-09  25-09-09  10:00 AM  11:00 AM  Class X   section A  preparation  Teacher 1
24-09-09  25-09-09  10:00 AM  11:00 AM  Class X   section A  preparation  Teacher 1  


with the change being that the start dates will increment and show +1 day until it becomes equal to the end date.

Thanks again
alok

Reply to alokk

19

alokk, on Sep 23, 2009 4:01:13 am BST

Sorry the start date on the last row should state 25-09-09

Reply to alokk

20

venkat1926, on Sep 24, 2009 1:53:31 am BST

I have added two more rows to check my macro. This is in SHEET1 as follows

Start Date End Date Start Time End Time Group Sub Group Details Manager
9/23/2009 9/25/2009 10:00 AM 11:00 AM Class X section 1 Goes for preparation Teacher 1
9/27/2009 9/29/2009 10:00 AM 11:00 AM class xi section 1 xxxxxxx teacher 2
10/2/09 10/6/2009 10:00 AM 11:00 AM class xi section 1 xxxxxxx teacher 2

carefully type this in sheet1

copy this data in sheet 2 also from A1 to retrieve for rechecking
KEEP SHEET1 AS ACTIVE SHEET
now run this macro "test" and see whether you get what you want in sheet1.

if you want to recheck.
1. run the macro "undo"
and then
2. run the macro "test"(ALWAYS KEEPING SHEET 1 AS ACTIVE SHEET)

Sub test()
Dim j As Integer, k As Integer, m As Integer, n As Integer
j = Range("a1").End(xlDown).Row
'j is hte lsst row
k = j
Do
If k = 1 Then Exit Do
m = Cells(k, "b") - Cells(k, "A")
'MsgBox m
'Range(Cells(k + 1, "A"), Cells(k + m, "A")).Select
Range(Cells(k + 1, "A"), Cells(k + m, "A")).EntireRow.Insert

For n = 1 To m
Cells(k, 1).EntireRow.Copy Cells(k + n, 1)
Next n

For n = 1 To m

Cells(k + n, 1) = Cells(k, 1) + n
Next n
k = k - 1
'MsgBox k
Loop
End Sub

Sub undo()
Worksheets("sheet1").Cells.Clear
Worksheets("sheet2").UsedRange.Copy Worksheets("sheet1").Range("A1")

End Sub

any bug post back. if ok confirm

Reply to venkat1926

22

ja, on Sep 30, 2009 12:42:00 pm BST

Hi, if you could email me direct on jashcroft@telkomsa.net, I can send you exact requirements and make a payment :) ... Cheers John

Reply to ja

23

theman, on Oct 29, 2009 8:03:06 pm GMT

I'm trying to do something very similar but MUCH simpler I think.

I would like to use cell A1 to put text into, Cell B1 to put the n number of times to repeat cell A1, and have the output added to column C

For example:

ColA      ColB      ColC
A1 text   5         A1 text
                        A1 text
                        A1 text
                        A1 text
                        A1 text


Then you leave Col C alone and clear out A and B. Put in new input to get the following for example:

ColA ColB ColC
A1new 7 A1 text
A1 text
A1 text
A1 text
A1 text
A1new
A1new
A1new
A1new
A1new
A1new
A1new
</code>

You can keep using it to create one big long column of entries. I'd gladly send someone some $$ via paypal for the help if it's done in the next few hours.

Reply to theman

24

 theman, on Oct 29, 2009 8:06:17 pm GMT

Last one got messed up, here's how it would look...

ColA     ColB       ColC
A1new     7       A1 text
                  A1 text
                  A1 text
                  A1 text
                  A1 text 
                  A1new
                  A1new
                  A1new
                  A1new
                  A1new
                  A1new
                  A1new

Reply to theman