【问题标题】:I am trying to sum the columns of specific headers in a particular row but I am getting total sum of all columns of that row irrespective of header我正在尝试对特定行中特定标题的列求和,但我得到该行所有列的总和,而与标题无关
【发布时间】:2018-03-29 08:01:23
【问题描述】:

'我正在尝试对特定行中特定标题的列求和,但我得到的是该行所有列的总和,而与标题无关。有人可以告诉我我的错误吗?请查看附件图片以获取示例输入输出。

Dim DSum As Integer
Dim PSum As Integer
With wsn
    NIMsLastRow = Worksheets("NIMSCarrierCount").Cells(Rows.Count, 1).End(xlUp).Row
    NIMsLastCol = Worksheets("NIMSCarrierCount").Cells(1, Columns.Count).End(xlToLeft).Column
    For j = 2 To NIMsLastRow
        DSum = 0
        PSum = 0
        For k = 2 To NIMsLastCol
            If .Cells(1, k).Value = "LTE 1900Deployed" Or "LTE 2500Deployed" Or "LTE 800Deployed" Or "UnassignedDeployed" Then
                DSum = DSum + CInt(.Cells(j, k).Value)
            End If
            If .Cells(1, k).Value = "LTE 1900Planning" Or "LTE 2500Planning" Or "LTE 800Deployed" Or "UnassignedPlanning" Then
                PSum = PSum + CInt(.Cells(j, k).Value)
            End If
        Next k
        .Cells(j, NIMsLastCol + 1).Value = DSum
        .Cells(j, NIMsLastCol + 2).Value = PSum
    Next j
End With

【问题讨论】:

    标签: excel vba for-loop if-statement


    【解决方案1】:

    我会考虑重写以使用 Select Case,这也将解决您的测试条件周围的错误。请记住在模块顶部使用 Option Explicit 来检查变量声明。您的 DSum 和 Psum 是否可能需要 Double?请注意,我已将整数交换为长整数以避免潜在的溢出(在尝试存储对于声明的数据类型而言太大的内容时会发生大量数字)

    Option Explicit 'Always use Option Explicit
    
    Sub test()
    
    Dim wsn As Worksheet
    Set wsn = ThisWorkbook.Worksheets("NIMSCarrierCount") 'assumption this is correct sheet assigment
    
    Dim DSum As Long 'use Long to avoid potential overflow
    Dim PSum As Long
    Dim NIMsLastRow As Long 'declare all variables
    Dim NIMsLastCol As Long
    Dim j As Long
    Dim k As Long
    
    With wsn
    
        NIMsLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        NIMsLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    
        For j = 2 To NIMsLastRow
    
            DSum = 0
            PSum = 0
    
            Dim testValue As String
    
            For k = 2 To NIMsLastCol
    
                testValue = .Cells(1, k)
    
                Select Case testValue
    
                Case "LTE 1900Deployed", "LTE 2500Deployed", "UnassignedDeployed"
                    DSum = DSum + CLng(.Cells(j, k))
    
                Case "LTE 1900Planning", "LTE 2500Planning", "UnassignedPlanning"
                    PSum = PSum + CLng(.Cells(j, k))
    
                Case "LTE 800Deployed"
    
                  DSum = DSum + CLng(.Cells(j, k))
                  PSum = PSum + CLng(.Cells(j, k))
    
                End Select
    
            Next k
    
            .Cells(j, NIMsLastCol + 1).Value = DSum
            .Cells(j, NIMsLastCol + 2).Value = PSum
    
        Next j
    
    End With
    
    End Sub
    

    【讨论】:

    • 方法比 OP 的要好得多。不过,这是一个合乎逻辑的问题:您在两种情况下都有 LTE 800Deployed (查看 OP 的问题,他们也是如此)。在这种情况下,如果场景是 LTE 800Deployed,它只会进入第一个 case 语句,永远不会遇到第二个 case。所以也许它应该是一个 IF 语句而不是一个 SELECT 因为我怀疑 OP 想要为 LTE 800Deployed 打两个 SUMS b>?
    • 对于 LTE 800Deployed 可能只有第三种情况,它将两个总和相加?
    • 感谢罗比、QHar 和 Zac。我是一名新手,我正在通过完成我的第一个 excel VBA 作业来学习 vba(以减轻我在 excel 中的任务)。如果不是 SO 和像你这样的人,我会永远迷路。太感谢了。 :-)
    • 不用担心。花点时间查看我上面的答案,因为还有一些额外的事情需要注意,例如Option Explicit,使用Long,声明所有变量,避免activesheet隐式引用。
    【解决方案2】:

    您的 if 语句写错了。

    Or "LTE 2500Deployed" 对于每个查询都被评估为 True。

    您需要完全指定每个参数 .Cells(1, k).Value = "LTE 1900Deployed" Or .Cells(1, k).Value = "LTE 2500Deployed" Or...

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多