我正在尝试确定合并单元格(它的开始单元格和结束单元格)的边界。
这是用于计划工具的,在这里我将这两行用作一种日历。我尝试使用.Count为起始单元格和结束单元格设置范围,但运气不好,因为两者都输出了起始单元格地址。
Set rDate = Sheet1.Range("G2:" & sCol & "3")
Set rMonth = rDate.Find(sMonth)
If rDate.MergeCells Then
Set rStart = rDate.Cells(1, 1)
Set rEnd = rDate.Cells(rDate.Rows.Count, rDate.Columns.Count)
End If
MsgBox rStart.Address
MsgBox rEnd.Address
两个范围都将G2输出为地址。我还附上了问题的图片。
答案 0 :(得分:7)
这就是Range.MergeArea
:
返回一个 Range 对象,该对象表示包含指定单元格的合并范围。如果指定的单元格不在合并范围内,则此属性返回指定的单元格。
If rDate.MergeCells ' not really necessary, unless you only want to work with a merged cell
MsgBox rDate.MergeArea.Address
End If
还要注意,您通过合并的单元格的最顶部,最左侧的单元格来引用:
Set rDate = Range("G2") 'this is still a merged cell.
如果要引用开始和结束单元格,也许是这样的:
Sub Test()
Dim rDate As Range
Set rDate = Sheet1.Range("G2")
If rDate.MergeCells Then
Dim startAddress As String
startAddress = Split(rDate.MergeArea.Address, ":")(0)
Dim endAddress As String
endAddress = Split(rDate.MergeArea.Address, ":")(1)
End If
MsgBox "The start address is " & startAddress & _
" and the end address is " & endAddress & "."
End Sub
答案 1 :(得分:2)
尝试
MsgBox rStart.Address
MsgBox rStart.MergeArea(rStart.MergeArea.Count).Address