【问题标题】:Sum a Range of Values conditionally有条件地对一系列值求和
【发布时间】:2018-11-05 14:36:00
【问题描述】:

我想对一系列值求和,不包括同一行中另一个单元格包含指定值的单元格。

我需要使用 VBA 在 Excel 中执行以下实现。

我有以下示例中报告的以下数据:

我需要天数的总和,不包括“测试”所需的天数。

我知道 offSet 做了类似的事情,但是,“测试”可以随机插入任何字段,因此,我需要动态计算。

所需的输出是 16。

【问题讨论】:

  • 为什么不直接使用 SUMIF?
  • 听起来像 SUMIF 的测试 "Test"

标签: excel vba


【解决方案1】:

对于这个例子,我使用 Sheet1,动态范围从 A2 到 LastRow。

试试:

Sub Test()
    Dim LR As Long
    Dim Total As Variant

    With Worksheets("Sheet1")
         LR = .Cells(.Rows.Count, "A").End(xlUp).Row
         Total = Application.WorksheetFunction.SumIf(.Range("A2:A" & LR), "<>Test", .Range("B2:B" & LR))
    End With
End Sub

【讨论】:

    【解决方案2】:

    数组输给 SumIf

    最搞笑的是排队时间更长

    vntArr = objRng
    

    (即将范围粘贴到数组中)而不是整个 SumIf Code 完成。

    在一百万行时,“Array”版本不到 4 秒,而“SumIf”版本不到 1.5 秒。

    '*******************************************************************************
    'Purpose:   Sums up a range of values excluding the values of cells where
    '           another cell in the same row contains a specified value.
    '*******************************************************************************
    Sub SumifArray()
    
      Const cstrName As String = "Sheet1" 'Name of the worksheet to be processed
      Const cLngFirstRow As Long = 2 'First row of data (excluding headers)
      Const cStrSumColumn As String = "B" 'The column to sum up
      Const cStrCheckColumn As String = "A" 'The column where to check against
      Const cStrCheckString As String = "Test" 'The value to be checked against
    
      Dim objRng As Range 'The range of data (both columns)
      Dim vntArr As Variant 'The array where the range is to be pasted into
      Dim lngLastRowCheck As Long 'Calculated last row of data in the "check" column
      Dim lngLastRowSum As Long 'Calculated last row of data in the "sum" column
      Dim lngArrCounter As Long 'Array row counter
      Dim lngSum As Long 'Value accumulator
    
      With Worksheets(cstrName)
        ' Last used row in column cStrCheckColumn
        lngLastRowCheck = .Columns(cStrCheckColumn).Find(What:="*", _
            After:=.Cells(1, cStrCheckColumn), LookIn:=xlFormulas, _
            Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        ' Last used row in column cStrSumColumn
        lngLastRowSum = .Columns(cStrSumColumn).Find(What:="*", _
            After:=.Cells(1, cStrSumColumn), LookIn:=xlFormulas, _
            Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      End With
    
      ' Calculate the range of data
      Set objRng = Range(Cells(2, cStrCheckColumn), _
          Cells(lngLastRowCheck, cStrSumColumn))
      ' Paste the range of data into an array (One-based, two-dimensional)
      vntArr = objRng
      ' Release object variable: the data is in the array
      Set objRng = Nothing
    
      ' Loop through the array
      For lngArrCounter = LBound(vntArr) To UBound(vntArr)
        ' Check if the value in the "check" column isn't equal to cStrCheckString
        If vntArr(lngArrCounter, 1) <> cStrCheckString Then _
            lngSum = lngSum + vntArr(lngArrCounter, 2)
      Next
    
      ' Write the result into the first empty row after the last row of data in
      ' the "sum" column
      Worksheets(cstrName).Cells(lngLastRowSum + 1, cStrSumColumn) = lngSum
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      您可以选择不相加的项目的替代代码:

      示例:在执行宏之前:

      你在这个例子中选择了项目(活动单元格)我选择了B(Total is 20 - B value(3) = 17)执行你得到的宏:

      代码sn-p是:

      Sub test()
      'i suppose the name are on the A column and the value are in b column
      Dim rows As Integer
      Dim sum As Double
      
      sum = 0
      
      'count how many rows
      rows = ActiveSheet.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).count
      
      For i = 2 To rows
      
          'sum the value
          sum = sum + Cells(i, 2) 'column B value
      
      Next i
      
      'subtract the item focused
      sum = sum - Cells(ActiveCell.Row, 2)
      
      'write sum in the last row (column B)
      Cells(rows + 1, 2) = sum
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-12
        • 1970-01-01
        相关资源
        最近更新 更多