PC Review


Reply
Thread Tools Rate Thread

Copy/Paste data based on a criteria

 
 
haas786@yahoo.com
Guest
Posts: n/a
 
      8th Feb 2008
Hi all!

I posted this earlier and haven't received any help and I kind of need
to have this done by the end of the day today. If you can please help
with this, it'll be truly appreciated. Here's what I have:

In cells A5 to A12, I have names of people (no
duplicates.) In cells B5 to B12 I have numbers next to the names. I
need to write a code which will start by allowing me to look at the
number in B5 and copy the name in A5 and start pasting it in cell D15
and then D16 and so on (down the row) as many times as represented by
the number in cell B5. Then, the code should look at cell B6 and
depending on the number in there, start pasting the name in A6 that
many times in whichever cell in column D is next empty. The code will
stop as soon as it encounters a 0 value in any cell in B5 to B12.


For the purposes of an example, please see the following:
A B
Dave 4
John 3
Brad 2
Jack 0
Jane 0
Bart 0
Kate 0
Kent 0


The code will essentially copy Dave first, and then paste it 4 times
in cells D15, D16, D17, and D18. Next, it will copy John 3 times and
paste it in D19, D20, and D21. Next it will go to Brad, and copy it 2
times, and paste it in D22 and D23. Next, it will go to Jack, and the
code will stop there becaus eit encounters a 0.


I hope the example helps illustrate what i need. If you have any
questions, please don't hesitate to ask.


 
Reply With Quote
 
 
 
 
stjori@hotmail.com
Guest
Posts: n/a
 
      8th Feb 2008
On Feb 8, 6:58 pm, "haas...@yahoo.com" <haas...@yahoo.com> wrote:
> Hi all!
>
> I posted this earlier and haven't received any help and I kind of need
> to have this done by the end of the day today. If you can please help
> with this, it'll be truly appreciated. Here's what I have:
>
> In cells A5 to A12, I have names of people (no
> duplicates.) In cells B5 to B12 I have numbers next to the names. I
> need to write a code which will start by allowing me to look at the
> number in B5 and copy the name in A5 and start pasting it in cell D15
> and then D16 and so on (down the row) as many times as represented by
> the number in cell B5. Then, the code should look at cell B6 and
> depending on the number in there, start pasting the name in A6 that
> many times in whichever cell in column D is next empty. The code will
> stop as soon as it encounters a 0 value in any cell in B5 to B12.
>
> For the purposes of an example, please see the following:
> A B
> Dave 4
> John 3
> Brad 2
> Jack 0
> Jane 0
> Bart 0
> Kate 0
> Kent 0
>
> The code will essentially copy Dave first, and then paste it 4 times
> in cells D15, D16, D17, and D18. Next, it will copy John 3 times and
> paste it in D19, D20, and D21. Next it will go to Brad, and copy it 2
> times, and paste it in D22 and D23. Next, it will go to Jack, and the
> code will stop there becaus eit encounters a 0.
>
> I hope the example helps illustrate what i need. If you have any
> questions, please don't hesitate to ask.


Try this:

Sub x()
Dim rng As Range
Dim r As Long

r = 15
For Each rng In Range("A5:A12")
If rng.Offset(, 1) <> 0 Then
Cells(r, "D").Resize(rng.Offset(, 1)) = rng
r = r + rng.Offset(, 1)
Else
Exit Sub
End If
Next rng

End Sub
 
Reply With Quote
 
haas786@yahoo.com
Guest
Posts: n/a
 
      8th Feb 2008
On Feb 8, 3:38*pm, stj...@hotmail.com wrote:
> On Feb 8, 6:58 pm, "haas...@yahoo.com" <haas...@yahoo.com> wrote:
>
>
>
>
>
> > Hi all!

>
> > I posted this earlier and haven't received any help and I kind of need
> > to have this done by the end of the day today. If you can please help
> > with this, it'll be truly appreciated. Here's what I have:

>
> > In cells A5 to A12, I have names of people (no
> > duplicates.) In cells B5 to B12 I have numbers next to the names. I
> > need to write a code which will start by allowing me to look at the
> > number in B5 and copy the name in A5 and start pasting it in cell D15
> > and then D16 and so on (down the row) as many times as represented by
> > the number in cell B5. Then, the code should look at cell B6 and
> > depending on the number in there, start pasting the name in A6 that
> > many times in whichever cell in column D is next empty. The code will
> > stop as soon as it encounters a 0 value in any cell in B5 to B12.

>
> > For the purposes of an example, please see the following:
> > * *A * * * * *B
> > Dave * * * *4
> > John * * * *3
> > Brad * * * *2
> > Jack * * * *0
> > Jane * * * *0
> > Bart * * * *0
> > Kate * * * 0
> > Kent * * * 0

>
> > The code will essentially copy Dave first, and then paste it 4 times
> > in cells D15, D16, D17, and D18. Next, it will copy John 3 times and
> > paste it in D19, D20, and D21. Next it will go to Brad, and copy it 2
> > times, and paste it in D22 and D23. Next, it will go to Jack, and the
> > code will stop there becaus eit encounters a 0.

>
> > I hope the example helps illustrate what i need. If you have any
> > questions, please don't hesitate to ask.

>
> Try this:
>
> Sub x()
> Dim rng As Range
> Dim r As Long
>
> r = 15
> For Each rng In Range("A5:A12")
> * * If rng.Offset(, 1) <> 0 Then
> * * * * Cells(r, "D").Resize(rng.Offset(, 1)) = rng
> * * * * r = r + rng.Offset(, 1)
> * * Else
> * * * * Exit Sub
> * * End If
> Next rng
>
> End Sub- Hide quoted text -
>
> - Show quoted text -



That works perfect. Thank you sooooo much! One last question - If the
first name (in cell A5) has a 0 next to it, I want to be able to go to
the second cell (A6) and start the loop from there. If A6 has a value
next to it, then i need it copied and pasted that many times. If it
has a 0 next to it, then i want the code to end right then and there.
Thanks again for your help...absolutely amazing!
 
Reply With Quote
 
stjori@hotmail.com
Guest
Posts: n/a
 
      8th Feb 2008
On Feb 8, 8:50 pm, "haas...@yahoo.com" <haas...@yahoo.com> wrote:
> On Feb 8, 3:38 pm, stj...@hotmail.com wrote:
>
>
>
> > On Feb 8, 6:58 pm, "haas...@yahoo.com" <haas...@yahoo.com> wrote:

>
> > > Hi all!

>
> > > I posted this earlier and haven't received any help and I kind of need
> > > to have this done by the end of the day today. If you can please help
> > > with this, it'll be truly appreciated. Here's what I have:

>
> > > In cells A5 to A12, I have names of people (no
> > > duplicates.) In cells B5 to B12 I have numbers next to the names. I
> > > need to write a code which will start by allowing me to look at the
> > > number in B5 and copy the name in A5 and start pasting it in cell D15
> > > and then D16 and so on (down the row) as many times as represented by
> > > the number in cell B5. Then, the code should look at cell B6 and
> > > depending on the number in there, start pasting the name in A6 that
> > > many times in whichever cell in column D is next empty. The code will
> > > stop as soon as it encounters a 0 value in any cell in B5 to B12.

>
> > > For the purposes of an example, please see the following:
> > > A B
> > > Dave 4
> > > John 3
> > > Brad 2
> > > Jack 0
> > > Jane 0
> > > Bart 0
> > > Kate 0
> > > Kent 0

>
> > > The code will essentially copy Dave first, and then paste it 4 times
> > > in cells D15, D16, D17, and D18. Next, it will copy John 3 times and
> > > paste it in D19, D20, and D21. Next it will go to Brad, and copy it 2
> > > times, and paste it in D22 and D23. Next, it will go to Jack, and the
> > > code will stop there becaus eit encounters a 0.

>
> > > I hope the example helps illustrate what i need. If you have any
> > > questions, please don't hesitate to ask.

>
> > Try this:

>
> > Sub x()
> > Dim rng As Range
> > Dim r As Long

>
> > r = 15
> > For Each rng In Range("A5:A12")
> > If rng.Offset(, 1) <> 0 Then
> > Cells(r, "D").Resize(rng.Offset(, 1)) = rng
> > r = r + rng.Offset(, 1)
> > Else
> > Exit Sub
> > End If
> > Next rng

>
> > End Sub- Hide quoted text -

>
> > - Show quoted text -

>
> That works perfect. Thank you sooooo much! One last question - If the
> first name (in cell A5) has a 0 next to it, I want to be able to go to
> the second cell (A6) and start the loop from there. If A6 has a value
> next to it, then i need it copied and pasted that many times. If it
> has a 0 next to it, then i want the code to end right then and there.
> Thanks again for your help...absolutely amazing!


This should do it.
Sub x()
Dim rng As Range
Dim r As Long

r = 15
For Each rng In Range("A5:A12")
If rng.Address = "$A$5" And rng.Offset(, 1) = 0 Then GoTo line0
If rng.Offset(, 1) <> 0 Then
Cells(r, "D").Resize(rng.Offset(, 1)) = rng
r = r + rng.Offset(, 1)
Else
Exit Sub
End If
line0:
Next rng

End Sub
 
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
Copy and Paste based on criteria stacia Microsoft Excel Programming 1 12th Feb 2010 05:42 PM
Copy and Paste based on criteria stacia Microsoft Excel Misc 1 8th Feb 2010 09:04 PM
Attempting to copy/paste data from one worksheet to another (same info but diff layout) based on date criteria tdb770 Microsoft Excel Programming 1 27th Feb 2007 03:17 PM
Copy/Paste based on Criteria Dan R. Microsoft Excel Programming 2 5th Feb 2007 08:25 PM
Help with copy/paste based on criteria Jayhawktc Microsoft Excel Programming 2 31st Mar 2004 04:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:35 AM.