【问题标题】:How to sum cells that don't contain a specific value如何对不包含特定值的单元格求和
【发布时间】:2021-01-13 10:05:54
【问题描述】:

如果 Excel 中的行与第二张表上的行不同,我会尝试从第一张表中计算行数。

但是有问题。

inicijaliDT 是我在Sheet2 上检查的数据范围。

inicijali = wsNOSTROSheet.Range("R" & brojac).Value
For Each Row In inicijaliDT
    If inicijali <> Row.Value Then
        brojRedaka = brojRedaka + 1
    'Else
        'Exit For
    End If
    Exit For
Next Row

【问题讨论】:

  • 不是很清楚你需要完成什么......每张纸的行数是否相同?您想比较第一张纸的第 1 行和第二张纸的第 1 行,依此类推吗?并计算不相同的行数?
  • (1) 第一次迭代后您将退出循环。
  • (2) 范围inicijaliDt 是单列范围吗? (3) 您应该考虑给变量起的名称(例如,Row 不是单元格的好名称)。

标签: excel vba sum


【解决方案1】:

如果我正确理解了您要完成的操作,请尝试下一个代码:

Sub testCompareRows()
 Dim sh1 As Worksheet, sh2 As Worksheet, lastR As Long, i As Long, cnt As Long
 
 Set sh1 = ActiveSheet 'use here your first sheet
 Set sh2 = sh1.Next    'use here your second sheet
 lastR = sh1.UsedRange.rows.count + sh1.UsedRange.row - 1
 For i = 1 To lastR
    With Application
        If Join(.Transpose(.Transpose(sh1.rows(i)))) <> _
            Join(.Transpose(.Transpose(sh2.rows(i)))) Then cnt = cnt + 1
    End With
 Next i
 If cnt > 0 Then MsgBox "There are " & cnt & " different rows..."
End Sub

您没有问澄清问题,代码假定两张表具有相同的行数,第一张表中的每个行号都将与第二张表中的相同行号进行比较...

【讨论】:

    【解决方案2】:

    计数不相等

    如果inicijaliDT单列 范围,您可以执行以下操作之一:

    Sub testLoop()
        Dim cel As Range
        Dim brojRedaka As Long
        inicijali = wsNOSTROSheet.Range("R" & brojac).Value
        For Each cel In inicijaliDT.Cells
            If cel.Value <> inicijali Then
                brojRedaka = brojRedaka + 1
            End If
        Next cel
        'Debug.Print brojRedaka
    End Sub
    
    Sub testCountIf()
        Dim brojRedaka As Long
        inicijali = wsNOSTROSheet.Range("R" & brojac).Value
        brojRedaka = inicijaliDT.Rows.Count _
            - Application.CountIf(inicijaliDT, inicijali)
        'Debug.Print brojRedaka
    End Sub
    

    如果inicijaliDT 是一个多列 范围并且您尝试计算在其任何单元格中未找到inicijali 的行数,您可以执行以下操作之一:

    Sub testRowLoop()
        Dim RowRange As Range
        Dim cel As Range
        Dim brojRedaka As Long
        Dim isFound As Boolean
        inicijali = wsNOSTROSheet.Range("R" & brojac).Value
        For Each RowRange In inicijaliDT.Rows
            For Each cel In RowRange.Cells
                If cel.Value = inicijali Then
                    isFound = True
                    Exit For
                End If
            Next cel
            If isFound Then
                isFound = False
            Else
                brojRedaka = brojRedaka + 1
            End If
        Next RowRange
        'Debug.Print brojRedaka
    End Sub
    
    Sub testRowMatch()
        Dim RowRange As Range
        Dim brojRedaka As Long
        inicijali = wsNOSTROSheet.Range("R" & brojac).Value
        For Each RowRange In inicijaliDT.Rows
            If IsError(Application.Match(inicijali, RowRange, 0)) Then
                brojRedaka = brojRedaka + 1
            End If
        Next RowRange
        ' Debug.Print brojRedaka
    End Sub
    

    【讨论】:

      【解决方案3】:

      这个你可以自定义。记得在运行之前配置代码中的值。

      'TASK: count rows in first sheet that 
      'have a different value to the same cells in the second sheet.
      
      Sub countRows()
      
      Dim positioner As Long, count As Long, last_row As Long
      Dim db_first_row As Byte, col_n As Byte, name_s1 As String
      Dim name_s2 As String, display_ans As String, msg As String
      Dim display_in_cell As Boolean, display_in_msgbox As Boolean
      
      'CONFIR HERE BEFORE YOU RUN IT
      '------------------------------------------------
      name_s1 = "Sheet1"         'name of first sheet
      name_s2 = "Sheet2"         'name of second sheet
      db_first_row = 2           'first row of the data set
      col_n = 2                  'colum number of the data set
      count = 0                  'from what number would you like to start counting?
      display_in_cell = True     'do you want to display the answer in a specific cell?
      display_ans = "C3"         'in what cell?
      display_in_msgbox = True   'do you want to display the answer in a msgbox?
      msg = "Different values counted: " 'message to display in msgbox
      '------------------------------------------------
      
      'get the last row of the data set
      last_row = Sheets(name_s1).Cells(Rows.count, col_n).End(xlUp).Row
      
      For positioner = db_first_row To last_row
          
          'if values of both sheets match in the same cell then count
          If Sheets(name_s1).Cells(positioner, col_n) <> _
          Sheets(name_s2).Cells(positioner, col_n) Then
                  count = count + 1
              End If
      
      Next positioner
      
      If display_in_cell Then
          Sheets(name_s1).Range(display_ans) = count
      End If
      
      If display_in_msgbox Then
          MsgBox msg & count
      End If
      
      End Sub
      

      【讨论】:

      • 谢谢,我没看到。
      • 一些温和的 cmets: 1- 您应该始终在 Dim 语句中声明每个变量的类型,否则,除了每行的最后一个变量之外,您的所有变量都将是 Variant 类型。 2- 当你想使用IntegerByte 时,你应该使用Long。 3-在您的If 条件中,如果变量的类型为Boolean,则不需要= True 部分
      • 感谢您的建议。他们非常有帮助。只是一个问题,为什么我要为一个可能不大于 10 的数字使用 Long 变量,作为 db_first_row?
      • 看看this answer为什么使用整数而不是长整数?
      • @VBasic2008 提供的链接值得一读,谢谢。
      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-23
      相关资源
      最近更新 更多