2015-06-20 80 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