如何用VBA反向遍历Excel选中区域的单元格?
反向遍历选中区域单元格的VBA方案
直接通过索引倒序循环就能实现,不用折腾数组反转,代码简单直接:
Dim cellCount As Long Dim i As Long Dim targetArea As Range Set targetArea = Selection.Areas(1) cellCount = targetArea.Cells.Count Debug.Print "Selected Range : " & targetArea.Address(False, False) ' 从最后一个单元格倒序遍历到第一个 For i = cellCount To 1 Step -1 Debug.Print targetArea.Cells(i).Address(False, False) Next i
说明:
- 先把选中的第一个区域赋值给变量
targetArea,简化后续调用 - 获取区域内的总单元格数
cellCount - 用
For循环从cellCount开始,步长设为-1,实现倒序遍历 - 通过
targetArea.Cells(i)直接访问对应位置的单元格,完全满足“从最后到第一个处理”的需求
内容的提问来源于stack exchange,提问作者C0ppert0p
相关产品推荐
相关产品推荐

