开发者

Need a vba macro to create a single column of data based on starting numbers and a block size

I'm sorry that this might be a bit task specific for me and not much use to others. I have found some code on the site that i have been able to adapt to sort my data in开发者_运维知识库to one column but I still have to create the starting point manually which is taking me hours.

Basically, my system exports a report of spare user id's but rather than listing them all I get the first number in column A, and the number of available id's in that block in column B.

So I am looking to get from......

A    B
100  4
120  1
130  3
156  4

To.....

A
100
101
102
103
120
130
131
132
156
157
158
159

Any help greatly appreciated thank you.

Glenn


Sub BreakOutIDs()

    Dim vaInput As Variant
    Dim aOutput() As Long
    Dim i As Long, j As Long
    Dim lCnt As Long

    'put existing table into an array
    vaInput = Sheet1.Range("A1:B4").Value

    'loop through "column A"
    For i = LBound(vaInput, 1) To UBound(vaInput, 1)

        'loop through as many values as in "column B"
        For j = 0 To vaInput(i, 2) - 1

            'increase the output array size and put the value in it
            'redim only lets you increase the last part of an array
            lCnt = lCnt + 1
            ReDim Preserve aOutput(1 To 1, 1 To lCnt)
            aOutput(1, lCnt) = vaInput(i, 1) + j
        Next j
    Next i

    'transpose the array from a row to a column and write to column D
    Sheet1.Range("D1").Resize(UBound(aOutput, 2), 1).Value = Application.WorksheetFunction.Transpose(aOutput)

End Sub

I put the output in Column D instead of A so it wouldn't overwrite your existing data. It will overwrite data in D, so adjust the last line to put it where you want. Also, adjust the vaInput line to get your real input range.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜