Copy and pasting based on criteria

H

haas786

Hi all!

You guys are very helpful and I was wondering if I can get some more
assistance. 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 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.

Thanks in advance for all your help!
 
G

Gleam

Please try this:
Option Explicit
Sub CopyNames()
Dim i1 As Integer, Name As String, n1 As Integer, p1 As Integer, j1 As
Integer
p1 = 15
For i1 = 5 To 12 Step 1
Name = Cells(i1, 1).Value
n1 = Cells(i1, 2).Value
If n1 = 0 Then
MsgBox "Zero found. Stopping."
Exit Sub
End If
For j1 = p1 To p1 + n1 - 1 Step 1
Cells(j1, 4).Value = Name
Next j1
p1 = p1 + n1 - 1
Next i1
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top