Excel将合并的行视为一个时对行进行计数

时间:2019-06-25 06:20:07

标签: excel

我正在尝试计算具有合并行的电子表格中的行。默认情况下,excel的ROWS函数对每个单元格进行计数,但是我需要一个函数将合并的单元格视为单行。

2 个答案:

答案 0 :(得分:2)

VBA脚本似乎是执行此操作的唯一方法。下面是一个可行的粗糙示例(尽管在处理多列的特殊情况下可能会失败):

Public Function MergedRowsCount(r As Range) As Long
    MergedRowCount = 0
    For i = 1 To r.Rows.Count
        MergeRowSize = r(i).MergeArea.Rows.Count
        If MergeRowSize > 1 Then
            i = i + MergeRowSize - 1
        End If
        MergedRowCount = MergedRowCount + 1
    Next i
    MergedRowsCount = MergedRowCount
End Function

enter image description here

答案 1 :(得分:0)

这可以帮助您:

代码:

Option Explicit

Sub test()

    Dim rng As Range, cell As Range
    Dim mrgCells As Long, UnmrgCells As Long, TotalCells As Long, mrgRows As Long, mrgColumns As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        Set rng = .Range("A1:B10")

        'Total number of cells in a range(DOES NOT includes merge cells)
        TotalCells = rng.Cells.Count

        UnmrgCells = 0
        mrgColumns = 0
        mrgRows = 0

        For Each cell In rng

            If cell.MergeCells Then
                mrgCells = mrgCells + 1
            Else
                UnmrgCells = UnmrgCells + 1
            End If

        Next cell

        For i = 1 To 10

            If .Range("A" & i).MergeArea.Rows.Count > 1 Then
                mrgRows = mrgRows + 1
            End If

            If .Range("A" & i).MergeArea.Columns.Count > 1 Then
                mrgColumns = mrgColumns + 1
            End If

        Next i

        Debug.Print "Total Number of Cells: " & TotalCells & vbNewLine & _
                    "Total Number of Merged Cells: " & mrgCells & vbNewLine & _
                    "Total Number of Merged Rows: " & mrgRows & vbNewLine & _
                    "Total Number of Merged Columns: " & mrgRows & vbNewLine & _
                    "Total Number of Non Merged cells: " & UnmrgCells

    End With

End Sub

结果:

enter image description here