简体   繁体   English

如何根据另一个范围内不同单元格的位置和值对范围内的特定单元格求和?

[英]How to SUM specific cells in a range depending on the posistion and value of different cells in another range?

The goal for this function in to sum specific cells in a range depending on the value and position of other cells in a different range.此函数的目标是根据不同范围内其他单元格的值和位置对范围内的特定单元格求和。

Here is an image so it is more clear:这是一张图片,所以它更清晰:

在此处输入图片说明

The answer for Column M would be = 2+4+6+9 = 21 The answer for Column P would be = 3+7+10 = 20 M 列的答案是 = 2+4+6+9 = 21 P 列的答案是 = 3+7+10 = 20

There are 20 different "Precio, Precio2, Precio3,..." that are under "Licitante 1, Licitante 2, ..." which is why I used CASES.在“Licitante 1, Licitante 2, ...”下有 20 种不同的“Precio, Precio2, Precio3,...”,这就是我使用 CASES 的原因。

Recap, if a cell in column "M" is >0 then the function should select the cell on column "H" which is on the same row, do this for all the cells in said range and add them.回顾一下,如果“M”列中的单元格>0,则该函数应选择同一行“H”列上的单元格,对所述范围内的所有单元格执行此操作并添加它们。

I have this so far:到目前为止我有这个:

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

I guess i am missing something in the part where all the cells that meet the criteria add up.我想我在满足条件的所有单元格加起来的部分遗漏了一些东西。

Thank you for your help.感谢您的帮助。

There's a ton of issues with the code you posted.您发布的代码有很多问题。 Try this and see if works.试试这个,看看是否有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM