LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Loop/copy rows variable times to new sheet

Sorry one of the carriage returns seems to have been deleted when thiss was
posted... And just a note don't try to copy something less than 1 time (which
really only makes sense anyway)

Sub test()
Call CopyTextMultipleTimes("Two", 2)
Call CopyTextMultipleTimes("Three", 3)

End Sub

Sub CopyTextMultipleTimes(ByVal TextToFind As String, ByVal Copies As Integer)
Dim wksCopyFrom As Worksheet
Dim wksCopyTo As Worksheet
Dim rngCopyFrom As Range
Dim rngToSearch As Range

Set wksCopyFrom = Sheets("Sheet1")
Set wksCopyTo = Sheets("Sheet2")

Dim rngCopyTo As Range
Dim rngCurrent As Range
Dim rngFirst As Range
Dim intCounter As Integer

Set rngToSearch = wksCopyFrom.Columns(1)
Set rngCurrent = rngToSearch.Find(TextToFind)
If Not rngCurrent Is Nothing Then
Set rngFirst = rngCurrent
Set rngCopyFrom = rngCurrent.EntireRow
Do
Set rngCopyFrom = Union(rngCurrent.EntireRow, rngCopyFrom)
Set rngCurrent = rngToSearch.FindNext(rngCurrent)
Loop Until rngCurrent.Address = rngFirst.Address

For intCounter = 1 To Copies
Set rngCopyTo = wksCopyTo.Range("A65536").End(xlUp).Offset(1, 0)
rngCopyFrom.Copy rngCopyTo
Next intCounter
End If

End Sub

--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

Try something like this. You will need to modify these two lines

Set wksCopyFrom = Sheets("Sheet1")
Set wksCopyTo = Sheets("Sheet2")

To be the sheet name you are copying from and the sheet name you are copying
to.

Sub test()
Call CopyTextMultipleTimes("Two", 2)
Call CopyTextMultipleTimes("Three", 3)

End Sub

Sub CopyTextMultipleTimes(ByVal TextToFind As String, ByVal Copies As Integer)
Dim wksCopyFrom As Worksheet
Dim wksCopyTo As Worksheet
Dim rngCopyFrom As Range
Dim rngToSearch As Range Set wksCopyFrom = Sheets("Sheet1")
Set wksCopyTo = Sheets("Sheet2")

Dim rngCopyTo As Range
Dim rngCurrent As Range
Dim rngFirst As Range
Dim intCounter As Integer

Set rngToSearch = wksCopyFrom.Columns(1)
Set rngCurrent = rngToSearch.Find(TextToFind)
If Not rngCurrent Is Nothing Then
Set rngFirst = rngCurrent
Set rngCopyFrom = rngCurrent.EntireRow
Do
Set rngCopyFrom = Union(rngCurrent.EntireRow, rngCopyFrom)
Set rngCurrent = rngToSearch.FindNext(rngCurrent)
Loop Until rngCurrent.Address = rngFirst.Address

For intCounter = 1 To Copies
Set rngCopyTo = wksCopyTo.Range("A65536").End(xlUp).Offset(1, 0)
rngCopyFrom.Copy rngCopyTo
Next intCounter
End If

End Sub

--
HTH...

Jim Thomlinson


"Patti" wrote:

I have a sheet in which every row needs to be copied to a new sheet, but a
variable number of times. Example (source sheet):

Column A Column B
"Two" Pete
"Three" John
"Three" Cindy

I want to look at *text* in column A and say "if A1 is Two then copy this
row to DestinationSheet 2 times, if text is Three copy 3 times." There
will only be 2 or 3 different conditions. When the loop is complete,
DestinationSheet would look like:

Column A Column B
"Two" Pete
"Two" Pete
"Three" John
"Three" John
"Three" John
"Three" Cindy
"Three" Cindy
"Three" Cindy

What is the most efficient way to do this?

Thanks in advance!

Patti



 
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
How do you copy a sheet times 50 tripflex Excel Discussion (Misc queries) 2 March 5th 09 11:01 PM
Insert Variable Number of Rows; With Loop ryguy7272 Excel Worksheet Functions 2 December 27th 06 08:25 PM
Loop Macro a variable number of times thesaxonuk Excel Discussion (Misc queries) 11 October 31st 06 06:05 PM
Loop thru rows to copy to another excel spreadsheet eighthman11 Excel Worksheet Functions 0 October 9th 06 09:21 PM
constructing a copy-paste loop that skips rows hm[_2_] Excel Programming 1 September 22nd 03 07:05 PM


All times are GMT +1. The time now is 12:45 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"