简单的Excel代码数组问题

所以我有2列A和B与他们的数字,这样的事情。

AB 96.66666667 0 193.3333333 0 290 0 386.6666667 0 483.3333333 1 725 22 966.6666667 19 1208.333333 10 

我想要的代码是看B列,如果它的值是0,不要说在C1列什么。 如果列B> 0,则在A中取对应的值,并列出A值,B次数。 因此,我在C中的最终目标列如下所示:

 483.3333333333333 725 725 725 725 etc til 725 happens 22 times then 966 966 etc until 966 happens 19 times etc. 

有任何想法吗?

作为一个快速的解决scheme,没有任何以后的转置,你可以使用这个:

 Sub test() Dim values As Variant, counter As Variant values = [A1:A100].Value 'change the ranges to fit your needs counter = [B1:B100].Value Dim output() As Variant ReDim output(0 To Application.WorksheetFunction.Sum(counter) - 1, 0) Dim i As Long, j As Long While i <= UBound(output) j = j + 1 For i = i To counter(j, 1) + i - 1 output(i, 0) = values(j, 1) Next Wend [C1].Resize(UBound(output) + 1).Value = output 'change C1 to the cell to start your output End Sub 

它生成一个数组( output ),它将简单地粘贴从C1开始(可以改变)

你可以用这个简单的代码来做到这一点:

 Sub trasposeNumbers() Dim i For Each i In Range("B1:B100") If i.Value = 0 Then 'do nothing Else Range(Cells(i.Row, 1), Cells(i.Row, i.Value)).Value = i.Offset(0, -1).Value End If Next i End Sub 

这样你重写列B中的值,只需注意。