VBA求和如果有多个条件

标签 vba excel excel-2003

我想知道是否有人可以帮助我。

大约一个星期以来,我一直在尝试整理一张包含基于多个标准的“SumIf”公式的表格。

我已经能够做到这一点,在单元格范围内手动输入具有所需结果的公式。

我遇到的问题实际上是两个方面。

  • 我使用的公式是数组公式,但我的一些用户的技能水平可能没有我希望的那么好,所以我知道他们自己无法应付添加公式,所以我查看了选项使用VB代码,
  • 问题是,我知道你不能在 VB 中使用数组公式,所以我有点不确定如何继续。

  • 为了更好地说明这一点,我附上了一个文件 here显示了我想要实现的目标。
  • 在工作表“幻灯片 5”上,我想提取结果
    橙色阴影列,
  • 对于每个单元格,我想执行以下“SumIf”:
  • 对指定范围内的数字求和 《实况》 ,
  • 如果命名范围内的值 "JRole" 行中的值相同6
    “幻灯片 5”表, ,
  • 命名范围内的值 “月” 与单元格 中的值相同B3
    “幻灯片 5”表, ,
  • 命名范围内的值 "PLOB"与单元格中的值相同
    K栏 “幻灯片 5”床单。

  • 正如我所说,我已经为此工作了一段时间,但我无法提出解决方案。

    我只是想知道是否有人可以看看这个,并就我如何能够实现这一点提供一些指导。

    非常感谢和亲切的问候

    最佳答案

    如果您想运行 Sub 来填充橙色字段,那么我的代码将为您填写数据

    Option Explicit
    
    Sub GetSolution()
    
        ' declaring a variable of Worksheet type for easier reference
        ' instead of saying: Sheets("All Data") you can use a shorter
        ' variable name: "data"
        Dim data As Worksheet
        Set data = Sheets("All Data")
    
        Dim result As Worksheet
        Set result = Sheets("Slide 5")
    
        ' these two variables hold the SUM for both analyst types
        ' in the loops flow
        Dim seniorAnalystSum As Double
        Dim analystSum As Double
    
        ' using resource and projectLob Range objects for quick reference to cells
        ' in both spreadsheets
        ' so again, instead of saying data.Range("A" & i) etc
        ' simple and short way is "resource" and/or "projectLob"
        Dim resource As Range
        Dim projectLob As Range
    
        ' for each cell in between K8:K16 on the result/ Sheets("Slide 5")
        ' pick up one cell at a time
        For Each resource In result.Range("K8:K16")
            ' and go over all cells between B5 and last cell in Data sheet
            For Each projectLob In data.Range("B5:B" & data.Range("B" & Rows.Count).End(xlUp).Row)
                If projectLob = resource Then
                    ' compare year and month between the matching cells
                    If (Month(projectLob.Offset(0, 8)) = Month(result.Range("B3"))) And _
                    (Year(projectLob.Offset(0, 8)) = Year(result.Range("B3"))) Then
                        ' compare to the row of the table that holds the type of analyst
                        If projectLob.Offset(0, 7) = result.Range("L6") Then
                            'C&R Senior Analyst
                            seniorAnalystSum = seniorAnalystSum + projectLob.Offset(0, 12)
                        ElseIf projectLob.Offset(0, 7) = result.Range("N6") Then
                            'C&R Analyst
                            analystSum = analystSum + projectLob.Offset(0, 12)
                        End If
                    End If
                End If
            Next
            ' Assigns the sum for the senior analyst
            resource.Offset(0, 2) = seniorAnalystSum
            ' assigns the sum for normal analyst
            resource.Offset(0, 4) = analystSum
            ' reset both sums for the next loop
            seniorAnalystSum = 0
            analystSum = 0
        Next
    
    End Sub
    

    我使用了您的示例数据并按如下方式填充了结果

    enter image description here

    我希望这是您正在寻找的解决方案 :) 等待您的反馈

    关于VBA求和如果有多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301709/

    相关文章:

    excel - 如何使用vba将access数据库(.accdb)中的数据检索到Excel中

    excel - 是否可以根据单元格边框的存在来显示值?

    macos - Mac 64 位编译器常量

    vba - VBA 中的 Workbooks.Open 方法

    java - 如何在 Java 中使用 Apache POI 框架创建 RichTextString?

    vba - 如何使用 VBA 仅复制值

    vba - 根据输入生成数据库中多个单元的排列

    VBA 将范围另存为工作簿

    javascript - 在 node.js 和 express 中下载 XLS

    excel - 格式条件和内部颜色