对于范围中的每个单元格,总计下一列单元格

时间:2019-06-12 18:15:32

标签: excel vba

对于A列中的每个“农场”;总计B列中“ apple-1”和“ apple-2”的C列(计数)中的值。

通过搜索和添加来循环。没有错误。数学是从错误的单元格发生的。

数据

Data table

Sub find_2()

Dim apple_1 As Long, apple_2 As Long
Dim j As Integer
Dim total_aps As Long

Dim name As Range
Dim myrange As Range

last_row = ActiveSheet.Range("A" & Rows.count).End(xlUp).Row

Set myrange = Range("A2:A17" & last_row)

Sheets("Sheet1").Activate

With ActiveSheet

    For j = 16 To last_row
        For Each name In myrange
              If .Cells(j, 2).Value = ("apple-1") Then
                apple_1 = Sheet1.Cells(j, 3).Value
              End If
              If .Cells(j, 2).Value = ("apple-2") Then
                apple_2 = Sheet1.Cells(j, 3).Value
              End If

    total_aps = apple_1 + apple_2 

        .Cells(j, 6).Value = total_aps

    Next

    Next j

End With

End Sub

=======================================================================


Expected/Wanted results

Farm       Total Count

farm-01    300

farm-02    30

farm-03    3

farm-04    3000

实际结果(请参阅总计) Actual results

0 个答案:

没有答案