简体   繁体   English

如何在 VBA 中循环范围?

[英]How to loop Ranges in VBA?

I am a newbee in VBA, I have a problem that how to how to loop ranges in VBA for example: Range("A1:D1").select to Range("A100:D100").select我是 VBA 中的新手,我有一个问题,即如何在 VBA 中循环范围,例如: Range("A1:D1").selectRange("A100:D100").select

or或者

Range("A1:D1").select to Range("D1:G1").select ? Range("A1:D1").selectRange("D1:G1").select ?

Note : it is better to avoid using Select , Activate , etc...注意:最好避免使用SelectActivate等...

To loop through ranges that changes per row, use the For loop through lRow below:要遍历每行更改的范围,请使用For循环lRow下面的lRow

' loop example per your case
For lRow = 1 To 100
    Range("A" & lRow & ":D" & lRow).Select
Next lRow

To loop through ranges that changes per column, use the For loop through Col below:要循环遍历每列更改的范围,请使用For循环遍历下面的Col

' loop example per your case
For Col = 1 To 4
    Range(Cells(1, Col), Cells(1, Col + 3)).Select
Next Col

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM