You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用更高效的VBA方法获取包含分散命名区域的矩形单元格区域?

获取非连续区域的包围矩形区域(VBA实现)

有更简洁高效的实现方式,无需手动遍历每个单元格,利用Excel内置的Application.Min和Application.Max函数直接获取目标区域的首末行、首末列,代码更简洁且执行效率更高(调用Excel内置计算逻辑,比VBA循环更快)。

代码示例

Dim firstRow As Long, lastRow As Long
Dim firstCol As Long, lastCol As Long
Dim rngRectangle As Range

' 获取包围矩形的首末行、首末列
firstRow = Application.Min(rngPM.Row)
lastRow = Application.Max(rngPM.Row)
firstCol = Application.Min(rngPM.Column)
lastCol = Application.Max(rngPM.Column)

' 构造包围矩形区域
Set rngRectangle = rngPM.Parent.Range(rngPM.Parent.Cells(firstRow, firstCol), _
                                      rngPM.Parent.Cells(lastRow, lastCol))

说明

  • 该方法直接利用Excel内置函数计算区域的边界行/列,避免了VBA循环遍历每个单元格的开销
  • 对于包含大量单元格的非连续区域,这种方式的效率优势会更明显

内容的提问来源于stack exchange,提问作者Dumitru Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 15:45:05