Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy rows and insert (x) number of times

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!!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Copy rows and insert (x) number of times

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.

" 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!!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Copy rows and insert (x) number of times

On Jan 13, 9:40*am, JLGWhiz 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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Copy rows and insert (x) number of times

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

" wrote:

On Jan 13, 9:40 am, JLGWhiz 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.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Copy rows and insert (x) number of times

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy a worksheet to new one with repeat rows 5 times mfn Excel Discussion (Misc queries) 0 February 13th 08 11:33 AM
Duplicate rows a given number of times in Excel? SB Excel Programming 3 February 8th 08 12:07 AM
Macro to copy exactly the I number of times that he/she appears in the column Israel[_2_] Excel Programming 1 March 17th 07 10:15 AM
how do i copy down x number of times Frazer Edwards Excel Worksheet Functions 1 July 6th 06 10:43 PM
Copy a formula down a set number of times Julian Excel Worksheet Functions 1 September 23rd 05 06:43 PM


All times are GMT +1. The time now is 05:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"