1

我有一列这样的数据:

Cage
1
1
1
1
1
1
1
Cage
1
1
1
1
Cage
1
1
1
1
1

我需要的是它旁边的另一列来说类似的话。

Cage
7
Cage
4
Cage
5

问题是信息每天都在变化,输入不能改变。1 是笼子里的东西。第二天可能是:

Cage 
1
Cage
1
1
Cage
1
1
1
4

1 回答 1

0

这是一个计算这个的过程:

Public Sub SumCages()
Dim current_row, summary_row, item_total As Integer

current_row = 1
While Sheet1.Cells(current_row, 1) <> ""
  If IsNumeric(Sheet1.Cells(current_row, 1)) Then
    item_total = item_total + Val(Sheet1.Cells(current_row, 1))
  Else
    summary_row = summary_row + 1 ' Advance summary_row
    If item_total > 0 Then
      Sheet1.Cells(summary_row, 2) = item_total ' Display total
      current_row = current_row - 1 ' Correct advancement
    Else
      Sheet1.Cells(summary_row, 2) = Sheet1.Cells(current_row, 1) ' Copy label
    End If
    item_total = 0 ' Reset item_total
  End If
  current_row = current_row + 1 ' Advance current_row
Wend
Sheet1.Cells(summary_row + 1, 2) = item_total

End Sub

在此处输入图像描述

您的数据必须符合问题细节:

  • 有一个非数字条目作为标签;
  • 每个标签下方的数字条目(可能不同于 1)。

具体到发布的文档,以下会产生预期的输出:

Public Sub SumCages()
Dim current_row, summary_row, item_total As Integer

current_row = 45
summary_row = 44
While Sheet8.Cells(current_row, 19) <> ""
  If IsNumeric(Sheet8.Cells(current_row, 19)) Then
    item_total = item_total + Val(Sheet8.Cells(current_row, 19))
  Else
    summary_row = summary_row + 1 ' Advance summary_row
    If item_total > 0 Then
      Sheet8.Cells(summary_row, 20) = item_total ' Display total
      current_row = current_row - 1 ' Correct advancement
    Else
      Sheet8.Cells(summary_row, 20) = Sheet8.Cells(current_row, 19) ' Copy label
    End If
    item_total = 0 ' Reset item_total
  End If
  current_row = current_row + 1 ' Advance current_row
Wend
Sheet8.Cells(summary_row + 1, 20) = item_total

End Sub
于 2013-08-29T02:36:59.420 回答