应用程序定义的或对象定义的错误合并单元
问题描述:
我有以下代码,要么采取ActiveCell和MsgBox
它的MergeArea
边界(工作正常),或采取预定的范围,并做相同的(失败)。当我尝试后者时,我得到application defined or object-defined
错误。应用程序定义的或对象定义的错误合并单元
Sub test2()
Dim Titles As Range
Set Titles = Range("E13:H13")
Dim titlesMerge As Range
' Set titlesMerge = ActiveCell.MergeArea 'this works fine
Set titlesMerge = Titles.MergeArea 'when use Range("E13:H13") it
'fails
MsgBox (titlesMerge.Row & " and " & titlesMerge.Rows.Count)
End Sub
单元格"E13:H13"
被合并。
这里有什么问题?
答
为了我对Siddharth Rout的评论的好奇心,我试验了这段代码。
Sub caller()
Dim rMergedCell As Range
' Let B3:C4 a merged cell
Set rMergedCell = Range("B3:C4")
Dim rTitlesCell As Range
Set rTitlesCell = rMergedCell.Cells(1, 1).MergeArea
MsgBox rTitlesCell.Row & " and " & rTitlesCell.Rows.Count
' Returns 3 and 2
End Sub
他指出,该MergeArea
属性仅适用于单一单元格引用。那么为什么不能获得合并单元格的最左上角的单元格?希望这有助于OP。
MergeArea属性仅适用于单细胞范围。 https://msdn.microsoft.com/en-us/library/office/ff822300.aspx –
因此,这解释了为什么'.MergeArea'与'ActiveCell'一起使用,而不是'Selection',即使它们引用相同的合并单元格。 –