2016-01-27 72 views
1

所以我有2列A和B在他們的數字,這樣的事情。簡單的Excel代碼陣列問題

A     B 
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. 

任何想法?

+2

是此作爲被粘貼的陣列(output)小的數據集和一次性的東西?因爲如果是這樣,你可以不用VBA。請參閱'REPT'函數(添加「,」爲單元格值),「文本到列」和「複製>過去>移調」。 –

回答

0

如沒有任何以後調換可以使用此一快速的解決方案:

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 

它產生這將簡單地用C1開始(可改變)

0

你可以用這個簡單的代碼做到這一點:

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列的值,只需注意。