首页 > 解决方案 > VBA 合并单元格的大小必须相同

问题描述

我有以下代码在电子表格中对数据、任务列表进行排序,首先它按任务优先级排序,将“已完成”推到底部,然后按日期排序,不包括已完成任务的任务;

Sub mcr_PushDown_Completed_Taks()
'
' Push down completed tasks

'

    'Turn off screen updating so macro working/flashing does not show
    Application.ScreenUpdating = False

    With Worksheets("Tasks")

        With .Range(.Cells(7, "A"), .Cells(.Rows.Count, "A").End(xlUp).Offset(0, 10))

            .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                        Orientation:=xlTopToBottom, Header:=xlYes

            With .Resize(Application.Match(7, .Columns(1), 0) - 1, .Columns.Count)

                .Cells.Sort Key1:=.Columns(2), Order1:=xlDescending, _
                            Orientation:=xlTopToBottom, Header:=xlYes

            End With

        End With

    End With

    Range("B7").Select

End Sub

但是,当我运行宏时突然出现错误

为此,所有合并的单元格必须具有相同的大小。

但是,我无法使用“查找”对话框在工作表上找到任何合并的单元格。我还能如何找到合并的单元格或解决这个问题?

标签: excel

解决方案


这个子将找到合并的单元格。它使用 . MergeArea查找合并单元格时的范围属性。VBA 不提供一个单元格属性,该属性会返回一个简单的“TRUE”到问题“我是合并区域的一部分”,因此如果单元格的区域与常规单元格相同,您需要自行评估或者如果它是更大区域的一部分。

Sub FindMergedCells()
Dim Rng As Range
Dim c As Range
Set Rng = Selection

With Rng
    For Each c In Rng
        If Not (c.MergeArea.Address = c.Address) Then
            Debug.Print c.Address & " is a merged cell"
        End If
    Next c
End With

End Sub

推荐阅读