如何使用VBA在Excel中遍历可变列长度范围 [英] How to iterate through a variable-column-length range in Excel using VBA

查看:318
本文介绍了如何使用VBA在Excel中遍历可变列长度范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果一个工作表中的数据如下所示:

For example, if the data in one worksheet looks like:

UsedRange.Columns.Count为6,这是所有行的最大列数. 即使我反复使用

UsedRange.Columns.Count is 6 which is the maximum column count for all rows. Even if I iterate using

For each row in UsedRange.Rows
       For each cell in row.Cells
           ...
       Next cell
Next row

每行仍在计数6.

推荐答案

如果单元格为空,只需退出单元格循环即可.

Just exit your cell loop if the cell is Empty.

For Each Row In UsedRange.Rows
   For Each cell In Row.Cells
       If IsEmpty(cell) Then
            Exit For
       End If

       'Do what you want here...

   Next cell
Next Row

这篇关于如何使用VBA在Excel中遍历可变列长度范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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