简体   繁体   English

Excel VBA sumif

[英]Excel VBA sumif

I have a working VBA script but I recently added a new IF statement and now it won't post the sumif value. 我有一个可以正常工作的VBA脚本,但是最近我添加了一个新的IF语句,现在它不会发布sumif值。 I'm trying to get the Lauren Comp section to populate the sum of that IF statement in the appropriate row in the AQ column. 我正在尝试获取Lauren Comp部分,以在AQ列的相应行中填充该IF语句的总和。

Sub CalculateCommission()
    Dim Index As Integer    'Row Index
    Dim employeeName As String 'A
    Dim compPlan As String  'B
    Dim baseWage As Double  'AM
    Dim commAN As Double 'AN
    Dim guarantee As Double 'AO
    Dim earningsDue As Double   'AQ
    Dim priorPay As Double 'AR from prior pay cycle
    Dim priorBase As Integer 'AS from prior pay cycle
    Dim newComm As Double   'AU
    Dim packBack As Double 'AP

    Index = 3

    Do While Len(ActiveSheet.Range("B" & Index))
        employeeName = UCase(ActiveSheet.Range("A" & Index))
        compPlan = UCase(ActiveSheet.Range("B" & Index))
        baseWage = ActiveSheet.Range("AM" & Index)
        newComm = ActiveSheet.Range("AU" & Index)
        earningsDue = ActiveSheet.Range("AQ" & Index)
        guarantee = ActiveSheet.Range("AO" & Index)
        packBack = ActiveSheet.Range("AP" & Index)

'set to zero if there's an error - based on prior pay cycle

        If (IsError(ActiveSheet.Range("AR" & Index)) = False) Then
            priorPay = ActiveSheet.Range("AR" & Index)
        Else
            priorPay = 0
        End If

        If (IsError(ActiveSheet.Range("AS" & Index)) = False) Then
            priorBase = ActiveSheet.Range("AS" & Index)
        Else
            priorBase = 0
        End If

        commAN = ActiveSheet.Range("AN" & Index)

'base plus commission,else

        If compPlan = "B" Or compPlan = "D" Or compPlan = "E" Or compPlan = "I" Or compPlan = "J" Or compPlan = "L" Then
                commAN = earningsDue - baseWage - guarantee - priorBase
            Else
                commAN = earningsDue - baseWage - guarantee
        End If

'Lauren comp

        If compPlan = "L" Then
                Sumact = Application.WorksheetFunction.SumIf(Worksheets("Calc by loan").Range("C:C"), "Doe, John", Worksheets("Calc by loan").Range("D:D"))
        End If
            ActiveSheet.Range("AQ" & Index).Value = Sumact

'base plus commission, else

        If compPlan = "B" Or compPlan = "D" Or compPlan = "E" Or compPlan = "I" Or compPlan = "J" Then
            If baseWage + newComm > baseWage Then
                earningsDue = baseWage + newComm + guarantee + priorBase - priorPay + packBack
            Else
                earningsDue = baseWage + guarantee + packBack
            End If
        Else
            If newComm > baseWage + priorPay Then
                earningsDue = newComm - priorPay + guarantee + packBack
            Else
                earningsDue = baseWage + guarantee + packBack
            End If
        End If
        ActiveSheet.Range("AQ" & Index).Value = earningsDue
        Index = Index + 1
    Loop
EndSub:
End Sub
ActiveSheet.Range("AQ" & Index).Value = Sumact
ActiveSheet.Range("AQ" & Index).Value = earningsDue

both look like they write to this cell. 两者看起来都像他们写入此单元格。 Are you sure in the "L" case , earnings due is not blank and overwriting your sumact value? 您确定在“ L”情况下,应收收入不是空白并且覆盖您的总价值吗? it does not look like there is any condition that would prevent the second line from running if the first line also ran. 如果第一行也运行,似乎没有任何条件会阻止第二行运行。

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

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