开发者

Excel macro, to copy and paste a cell value based on another cell value?

开发者 https://www.devze.com 2022-12-25 03:06 出处:网络
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

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开发者_如何学JAVA

A       B
DE43PP
DE43PP
DE43PP
DE43PQ
...

Thanks


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号