2015-06-20 83 views
-1

編程相當新穎。我有excel中的數據,由每個組的頂部和底部的空單元格分隔。 (example, A1=empty, A2=a number, A3= a number, A4= a number, A5=Empty, A6=a number, A7=a number, A8=Empty)此模式重複爲數千行。VBA Excel自動選擇公式使用的空間分隔數據範圍

我想使用vba獲取每組空單元格之間的數字的總和,並將該值6個單元格放在每個組的最頂部空單元格的右側。

我會手工做到這一點,但有一個TON的數據。它需要繼續,直到它達到2行中的空單元格(表示數據結束)。在此先感謝

+0

嗨泰勒,因爲你沒有提到或包括特定問題或代碼,我只能給建議,在那裏你能以某種方式得到的可能解。如果您的數據與您所描述的一樣統一,則可以通過公式手動執行此操作(在需要放置價值的第一個空間上)。然後自動填充公式。將其複製並粘貼爲值。然後篩選您的數據並選擇所有非空白,然後刪除這些數據的總和。這會給你你想要的。 – L42

+0

對不起,我不是說每組的數據點數是一致的。我只是指上面和下面的空單元格。 – Tyler

回答

0

這裏是要幫助例如:

Sub SumGroups() 
    Dim i As Long ' rows counter 
    Dim j As Long ' first row of current group, having empty cell 
    Dim s As Double ' summ 
    i = 1 ' start row 
    Do Until Cells(i, 1).Value = "" ' loop to first group begin 
     i = i + 1 ' next row 
    Loop 
    j = i ' new group start 
    s = 0 
    Do 
     If Cells(i, 1).Value = "" Then ' empty cell found 
      If j = i - 1 Then Exit Do ' finish if previous cell is also empty 
      Cells(j, 2).Value = s ' put calculated summ at the top of the group 
      j = i ' new group start 
      s = 0 
     Else ' value cell found 
      s = s + Cells(i, 1).Value ' add each value in the group 
     End If 
     i = i + 1 ' next row 
    Loop 
End Sub 
+0

非常感謝!學習曲線非常陡峭,但我很緊張 – Tyler