tags:

views:

393

answers:

1

Hi I shall try to explain clearly what I need to be able to do, here goes:

I have an Excel spread sheet 1 with postcodes in column A and a number in column B. I need to create a macro/formula so that it will see the number (i.e 3) and copy and paste the postcode that number of times into sheet 2 column a underneath each other. I need to do this for the next row down etc until it comes to a blank.

Sheet 1

A       B
DE43PP  3
DE43PQ  8

Sheet 2

A       B
DE43PP
DE43PP
DE43PP
DE43PQ
...

Thanks

+1  A: 

Try this:

Sub copyPostcodes()
    Dim c As Range
    Dim x As Long
    Dim y As Long

    y = 0

    For Each c In Sheets("Sheet1").Range("A:A").Cells
        If Len(c) > 0 Then
            For x = 1 To c.Offset(0, 1)
                Sheets("Sheet2").Range("A1").Offset(y, 0) = c
                y = y + 1
            Next x
        End If
    Next c
End Sub
Lunatik
Thank you that works perfectly!
Keziah Dorman
Just another query on this-if I wanted it to copy the whole row how would I edit the macro? Thanks
Keziah Dorman
@Keziah Dorman: Do you want to copy the number in column `B` too? Once your problem is solved, you should accept this answer (and I would up-vote it too).
Peter Lang
Thanks I have done that. Yes I would like to copy the column B and potentially C etc? It would just be very useful to know if there is a relatively easy edit to do this? Thanks
Keziah Dorman