Excel VBA中,怎么判断选定的区域是否连续

2025-04-19 23:15:32
推荐回答(1个)
回答1:

Selection.Areas.Count返回区域数,值为1时连续。    

用union连接不相邻区域。

下面是一段示例代码:

fr01 = Selection.Cells(1).Row
fr02 = Selection.Cells(Selection.Cells.Count).Row
fr03 = Selection.Rows.Count
fc01 = Selection.Cells(1, 1).Offset(rowoffset:=fr03 - 1).Row

If fc01 <> fr02 Then

    MsgBox ("该功能仅支持选中连续区域,请勿跳跃选中单元格")
    Exit Function
    
Else