excel - 找到合并单元格的边界?

标签 excel vba

我正在尝试确定合并单元格的边界(它是起始单元格和结束单元格)。

这是一个日程安排工具,我使用这两行作为一种日历。我尝试使用 .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 作为地址。我还附上了问题的图片。

enter image description here

最佳答案

这就是 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/

相关文章:

excel - 将数据从已发布的 Google 电子表格导入 Excel

excel - 每 5 行隐藏 4 个

excel - 提高从 Excel 到 Matlab 读取数据的速度

excel - 如何在工作表(不是工作簿)中存储和使用 VBA 函数

VBA Intellisense 仅在第一个周期后显示,在键入第一个周期后没有级别

vba - 制作一个要求一天的 Excel 宏,然后将其插入到下一个打开的单元格中

excel - VBA 应用程序定义的错误 Outlook 连接

vba - 使用 vba 在 Excel 中对形状进行分组和命名

mysql - 我可以加速查询 (AWS) mysql 数据库的 VBA 函数吗?

vba - 索引到大的不连续范围