【问题标题】:How to SUM specific cells in a range depending on the posistion and value of different cells in another range?如何根据另一个范围内不同单元格的位置和值对一个范围内的特定单元格求和?
【发布时间】:2018-08-02 18:28:57
【问题描述】:

此函数的目标是根据不同范围内其他单元格的值和位置对某个范围内的特定单元格求和。

这是一张图片,所以更清楚:

第 M 列的答案是 = 2+4+6+9 = 21 P 列的答案是 = 3+7+10 = 20

“Licitante 1, Licitante 2, ...”下有 20 种不同的“Precio, Precio2, Precio3,...”,这就是我使用 CASES 的原因。

回顾一下,如果“M”列中的一个单元格 >0,那么函数应该选择同一行上的“H”列上的单元格,对所述范围内的所有单元格执行此操作并添加它们。

到目前为止我有这个:

Function ImporteLic(lic As String)

Dim cell As Range
Dim i As Integer

Select Case lic

    Case "Licitante 1"
        For Each cell In Range("M13:M50")
            If ActiveCell.Value > 0 Then
                ActiveCell.Offset(0, -5) = i
            End If
        Next cell

    ImporteLic = worksheetfuntion.Sum(i)


End Select
End Function

我想我在满足条件的所有单元格加起来的部分中遗漏了一些东西。

感谢您的帮助。

【问题讨论】:

  • 你试过Application.SUMIFS()吗?

标签: excel vba sum


【解决方案1】:

您发布的代码存在大量问题。试试这个,看看是否有效。

Function ImporteLic(lic As String) As Double

    With Worksheets("Sheet1") 'change name

        Dim col As Long
        col = .Rows(1).Find(lic, lookat:=xlWhole).Column

        Dim checkRange As Range
        Set checkRange = .Range(.Cells(3, col), .Cells(50, col))

        Dim sumRange As Range
        Set sumRange = .Range("H3:H50")

        Dim addEmUp As Double
        addEmUp = WorksheetFunction.SumIf(checkRange, ">0", sumRange)

    End With

  ImporteLic = addEmUp

End Function

【讨论】:

  • 它成功了,非常感谢你,如果你有时间,你介意给我一些关于为什么我的代码有问题的指示吗?我是新手,所以努力学习,再次感谢!
猜你喜欢
  • 2019-11-22
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多