Get start range and end range of a vertically merged cell with Excel using VBA

前端 未结 6 881
有刺的猬
有刺的猬 2020-12-10 13:02

I need to find out the first cell and the last cell of a vertically merged cell..

Let\'s say I merge Cells B2 down to B50.
How can I get in VBA the start cell(=B

6条回答
  •  一向
    一向 (楼主)
    2020-12-10 13:45

    Well, assuming you know the address of one of the cells in the merged range, you could just select the offset from that range and get the row/column:

    Sub GetMergedRows()
        Range("A7").Select 'this assumes you know at least one cell in a merged range.
        ActiveCell.Offset(-1, 0).Select
        iStartRow = ActiveCell.Row + 1
        Range("A7").Select
        ActiveCell.Offset(1, 0).Select
        iEndRow = ActiveCell.Row - 1
        MsgBox iStartRow & ":" & iEndRow
    End Sub
    

    The code above will throw errors if the offset row cannot be selected (i.e. if the merged rows are A1 through whatever) so you will want to add error handling that tells the code if it can't offset up, the top rows must be 1 and if it can't go down, the bottom row must be 65,536. This code is also just one dimensional so you might want to add the x-axis as well.

提交回复
热议问题