如何跳过For Each循环语句中范围内的第一个元素? [英] How to skip first element in the range in a For Each loop statement?

查看:45
本文介绍了如何跳过For Each循环语句中范围内的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事情相对简单.我有一个根据条件过滤的数据集,因此隐藏了工作表中的某些行.我为过滤后的数据设置了一个范围,该范围仅应通过示例代码可见的单元格进行.

The thing I want to do is relatively simple. I have a data set which I filtered based on a criteria, hence hiding some of the rows in my worksheet. I set a range for my filtered data which should only go through cells that are visible with sample code.

With MyDataWorksheet.AutoFilter.Range
     On Error Resume Next
     Set AutoFilterRange = .SpecialCells(xlCellTypeVisible)
     On Error GoTo 0
End With

现在,我想遍历AutoFilterRange变量中的所有数据,该变量应该捕获了所有可见行.我让他们做这样的事情.

Now I would like to loop through all my data in the AutoFilterRange variable which should have captured all rows that are visible. I Loop through them doing something like this.

Sub aSub()

Dim DR As Range
For Each DR In AutoFilterRange
    'Do something here
Next DR

End Sub

我在每个循环中都使用它来处理可见行,但是我想跳过数据范围中的第一个元素,因为该元素行号包含我的标头名称.我以为这样做会帮助解决我的问题,但是它所做的只是转到标头行元素之后的下一个隐藏行元素.

I use this for each loop to do stuff with the visible rows, however I would like to just skip the first element in the data range as this element row number contains my header names. I thought doing something like this would help solve my problem, but all it does is go to the next hidden row element right after the header row element.

For Each DR In AutoFilterRange.Offset(1,0)
    'Do something here
Next DR

推荐答案

类似这样的东西

With MyDataWorksheet
    If .AutoFilterMode Then
        With .AutoFilter.Range
            Set AutoFilterRange = .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible)
        End With
    End If
End With

在您的代码中,如果AutoFilter未启用,则不会显示AutoFilterRange是什么,因此我也跳过了这一部分.

In your code you don't show what AutoFilterRange would be if AutoFilter is not On, so I also skip that part.

这篇关于如何跳过For Each循环语句中范围内的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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