当给定 Range 对象时,循环遍历单元格范围内的每个单元格 [英] Loop through each cell in a range of cells when given a Range object
本文介绍了当给定 Range 对象时,循环遍历单元格范围内的每个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下代码:
Sub TestRangeLoop()
Dim rng As Range
Set rng = Range("A1:A6")
''//Insert code to loop through rng here
End Sub
我希望能够为 rng
中指定的每个单元格遍历一组 Range
对象.从概念上讲,我想这样做:
I want to be able to iterate through a collection of Range
objects for each cell specified in rng
. Conceptually, I'd like to do it like so:
For Each rngCell As Range in rng
''//Do something with rngCell
Next
我知道我可以通过解析 rng.Address
并手动构建 Range
对象来解决这个问题,但我希望有一种更直接的方法,不涉及字符串解析.
I know I could solve this by parsing rng.Address
and building Range
objects manually, but I'm hoping there is a more direct way that doesn't involve string parsing.
推荐答案
Sub LoopRange()
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("A1:A6")
For Each rCell In rRng.Cells
Debug.Print rCell.Address, rCell.Value
Next rCell
End Sub
这篇关于当给定 Range 对象时,循环遍历单元格范围内的每个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文