当给定一个Range对象时,循环遍历单元格范围内的每个单元格 [英] Loop through each cell in a range of cells when given a Range object

查看:444
本文介绍了当给定一个Range对象时,循环遍历单元格范围内的每个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

  Sub TestRangeLoop()
Dim rng As Range
设置rng =范围(A1:A6)

''//插入代码循环通过rng这里
End Sub

我想能够遍历<$ c $中指定的每个单元格的 Range 对象的集合$ C> RNG 。在概念上,我想这样做:

 对于每个rngCell As rng 
''/ /做一些与rngCell
下一个

我知道我可以解决这个通过解析 rng.Address 并手动构建范围对象,但我希望有一个更直接的方式,不涉及字符串解析

解决方案

  Sub LoopRange()

Dim rCell As范围
Dim rRng As Range

设置rRng = Sheet1.Range(A1:A6)

每个rCell在rRng.Cells
调试。打印rCell.Address,rCell.Value
下一个rCell

End Sub


Let's say I have the following code:

Sub TestRangeLoop()
    Dim rng As Range
    Set rng = Range("A1:A6")

    ''//Insert code to loop through rng here
End Sub

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆