我正在尝试确定合并单元格的边界(它是起始单元格和结束单元格)。
这是一个日程安排工具,我使用这两行作为一种日历。我尝试使用 .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 作为地址。我还附上了问题的图片。
最佳答案
这就是 Range.MergeArea
是:
Returns a Range object that represents the merged range containing the specified cell. If the specified cell isn't in a merged range, this property returns the specified cell.
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
关于excel - 找到合并单元格的边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531512/