这是上一个问题(If cell contains value n, how can I create a column of n cells, each containing value n (and then repeat with another cell)?)的续篇。我有一列数字,由x行x值组成,然后y行y值,依此类推;举个简单的例子:
2
2
5
5
5
5
5
2
2
3.
3.
3.
(etc -实际列表超过2500行,包含2到200之间的值。)
我需要的是一个宏,它会递增地对数字的每次出现进行编号,每当值发生变化时将计数重置为1。因此上面的列表将生成如下所示的列:
2-1
2-2
5-1
5-2
5-3
5-4
5-5
2-1
2-2
3-1
3-2
3-3
..etc。
我猜想这个列是x行x行的值x,y行y的值y(等)并不是那么重要,但我提到它是为了以防万一。我使用的是Excel2000(对VBA几乎一无所知)。欢迎任何帮助。
发布于 2013-04-27 23:51:28
如果您需要更早的宏来满足此要求,您可以使用此版本,它在每个重复项旁边都包含了实例ID。
Public Sub Test()
PopulateColumn Range("A1"), Range("B1")
End Sub
Public Sub PopulateColumn(ByVal rngSourceStart As Range, ByVal rngTargetStart As Range)
Dim rngSource As Range
Dim rngTarget As Range
Set rngSource = rngSourceStart
Set rngTarget = rngTargetStart
Dim iValue As Integer
Dim i As Integer
While rngSource.Value <> ""
iValue = rngSource.Value
For i = 1 To rngSource.Value
rngTarget.Value = iValue
rngTarget.Offset(0, 1).Value = i
Set rngTarget = rngTarget.Offset(1, 0)
Next
Set rngSource = rngSource.Offset(1, 0)
Wend
End Sub
发布于 2013-04-27 23:36:59
如果你可以使用一个公式,你可以使用这个:
=if(A2=A1,B1+1,1)
假设第一个数字(2)在单元格A1中,将其放入单元格B2中。您只需将1
放在单元格B1中,然后在底部填充公式。
https://stackoverflow.com/questions/16253830
复制相似问题