PC Review


Reply
Thread Tools Rate Thread

Copy rows and insert (x) number of times

 
 
eric.d.soelberg@gmail.com
Guest
Posts: n/a
 
      13th Jan 2009
I am trying to copy two areas of my spreadsheet and insert those two
areas "X" number of times. I am novice with VBA and tried to take two
strings of code I found on the internet and splice them together, but
to no avail. Here's what I have right now, which is popping up a
prompt asking number of rows, and then it proceeds to copy the two
selections I want and insert them, just like I want it to for ONE
insert. Anyone know how I can get it to insert "X" number of rows
(the number entered into the prompt)?

Here's my current code:

Sub InsertRow()
Dim Rng
Rng = InputBox("How many additional states?")
Range("42:52").Copy
Rows("53").Select
Selection.EntireRow.Insert
Rows("30").Copy
Rows("31").Select
Selection.EntireRow.Insert
Application.CutCopyMode = False
End Sub


Any help is much appreciated!!
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      13th Jan 2009
There is nothing in the posting to indicate the criteria for selecting either
the rows to copy or the row locations to insert. In fact, it looks as if
those selections are arbitrary. If you can explain what determines which row
to copy and how to determine where to insert them, maybe someone can provide
some code. I can see no connection between the inputbox value and any of the
ranges copied or inserted.

"(E-Mail Removed)" wrote:

> I am trying to copy two areas of my spreadsheet and insert those two
> areas "X" number of times. I am novice with VBA and tried to take two
> strings of code I found on the internet and splice them together, but
> to no avail. Here's what I have right now, which is popping up a
> prompt asking number of rows, and then it proceeds to copy the two
> selections I want and insert them, just like I want it to for ONE
> insert. Anyone know how I can get it to insert "X" number of rows
> (the number entered into the prompt)?
>
> Here's my current code:
>
> Sub InsertRow()
> Dim Rng
> Rng = InputBox("How many additional states?")
> Range("42:52").Copy
> Rows("53").Select
> Selection.EntireRow.Insert
> Rows("30").Copy
> Rows("31").Select
> Selection.EntireRow.Insert
> Application.CutCopyMode = False
> End Sub
>
>
> Any help is much appreciated!!
>

 
Reply With Quote
 
eric.d.soelberg@gmail.com
Guest
Posts: n/a
 
      13th Jan 2009
On Jan 13, 9:40*am, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> There is nothing in the posting to indicate the criteria for selecting either
> the rows to copy or the row locations to insert. *In fact, it looks as if
> those selections are arbitrary. *If you can explain what determines which row
> to copy and how to determine where to insert them, maybe someone can provide
> some code. *I can see no connection between the inputbox value and any of the
> ranges copied or inserted.
>



Thanks for the reply. I'll try and clarify better.

I have a template in excel, which can remain static, except for data
in row 30 and rows 42-52, which I am trying to be able to copy and
insert (X) number of times, depending on my needs. For example, I am
trying to be able to get the prompt to pop up when I click a button
asking me how many rows I need, and then if I type "5", five rows of
row 30 will be copied and insterted below row 30 and five copies of
rows 42-52 will be copied below those rows (starting on row 53).

I got this code online:
Sub InsertRow()
Dim Rng
Rng = InputBox("Enter number of rows required.")
Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(Rng - 1, 0)).Select
Selection.EntireRow.Insert
End Sub

This code got the prompt to pop up and put (X) numbers of rows from
the active cell - but no copying was done and I want it to be able to
just always do (X) versions of row 30 below row 30 and (X) versions of
rows 42-52 below those rows.

So by recording my own macro and then trying to use the one pasted
above, I came up with the code originally posted. I know I need to
use the rng code still, probably combined with "offset", but when I
tried to replace the term "ActiveCell" with the rows I want copied, it
gave me errors.

Hopefully this helps, and THANK YOU to anyone who can help correct my
code.

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      13th Jan 2009
Try this on a test page before installing it for permanent use.

The code is to be pasted into the standard code module1. Press Alt + F11 tp
access tje VBE.

Sub multicopy()
Dim x As Long, y As Long
numb = InputBox("Enter number of times to insert.", "Iteration")
x = CLng(numb)
Rows("42:52").Copy
y = Rows("42:52").Rows.Count * x
Range("A53").Resize(y, 1).Insert
Rows(30).Copy
Range("A31").Resize(x, 1).Insert
Application.CutCopyMode = False
End Sub

"(E-Mail Removed)" wrote:

> On Jan 13, 9:40 am, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> > There is nothing in the posting to indicate the criteria for selecting either
> > the rows to copy or the row locations to insert. In fact, it looks as if
> > those selections are arbitrary. If you can explain what determines which row
> > to copy and how to determine where to insert them, maybe someone can provide
> > some code. I can see no connection between the inputbox value and any of the
> > ranges copied or inserted.
> >

>
>
> Thanks for the reply. I'll try and clarify better.
>
> I have a template in excel, which can remain static, except for data
> in row 30 and rows 42-52, which I am trying to be able to copy and
> insert (X) number of times, depending on my needs. For example, I am
> trying to be able to get the prompt to pop up when I click a button
> asking me how many rows I need, and then if I type "5", five rows of
> row 30 will be copied and insterted below row 30 and five copies of
> rows 42-52 will be copied below those rows (starting on row 53).
>
> I got this code online:
> Sub InsertRow()
> Dim Rng
> Rng = InputBox("Enter number of rows required.")
> Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(Rng - 1, 0)).Select
> Selection.EntireRow.Insert
> End Sub
>
> This code got the prompt to pop up and put (X) numbers of rows from
> the active cell - but no copying was done and I want it to be able to
> just always do (X) versions of row 30 below row 30 and (X) versions of
> rows 42-52 below those rows.
>
> So by recording my own macro and then trying to use the one pasted
> above, I came up with the code originally posted. I know I need to
> use the rng code still, probably combined with "offset", but when I
> tried to replace the term "ActiveCell" with the rows I want copied, it
> gave me errors.
>
> Hopefully this helps, and THANK YOU to anyone who can help correct my
> code.
>
>

 
Reply With Quote
 
Eric S
Guest
Posts: n/a
 
      13th Jan 2009
Thanks JLGWhiz - worked perfectly. If you don't mind my asking, how
did you get so good at this? OJT? Reading books? I'm realizing the
potential of VBA to do some amazing things and want to get better...

Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto copy and insert a defined number of rows as defined in a cell Billy Microsoft Excel Programming 1 18th Mar 2010 10:48 PM
copy a worksheet to new one with repeat rows 5 times mfn Microsoft Excel Misc 0 13th Feb 2008 11:33 AM
Duplicate rows a given number of times in Excel? =?Utf-8?B?U0I=?= Microsoft Excel Programming 3 8th Feb 2008 12:07 AM
how do i copy down x number of times =?Utf-8?B?RnJhemVyIEVkd2FyZHM=?= Microsoft Excel Worksheet Functions 1 6th Jul 2006 10:43 PM
Copy Rows and insert these rows before a page break AQ Mahomed Microsoft Excel Programming 0 8th Jun 2004 09:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:23 AM.