【发布时间】:2019-08-21 00:22:56
【问题描述】:
我有列参考、计数和代码。我正在一一过滤列代码,过滤数据后,我只想获取可见行的值。有了这个,我使用了 SpecialCells(xlCellTypeVisible)。
在列代码中,过滤器中的第一个选择是“IG”。所以这只会留下我的数据的第一行,其余的都会隐藏在它下面。以下是我的数据:
|---------------------|------------------|------------------|
| Reference | Count | Code
|---------------------|------------------|------------------|
| A1 | 4 | IG
|---------------------|------------------|------------------|
| A2 | 3 | IH
|---------------------|------------------|------------------|
| A2 | 5 | IH
|---------------------|------------------|------------------|
| A2 | 6 | IH
|---------------------|------------------|------------------|
| A2 | 8 | IH
|---------------------|------------------|------------------|
| A2 | 8 | IH
|---------------------|------------------|------------------|
| A2 | 8 | IH
|---------------------|------------------|------------------|
| A3 | 8 | II
|---------------------|------------------|------------------|
| A3 | 10 | II
|---------------------|------------------|------------------|
| A3 | 11 | II
|---------------------|------------------|------------------|
| A4 | 15 | VO
|---------------------|------------------|------------------|
这里是我试过的代码:
Sub Try()
Dim cl As Range, rng As Range
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set StartCell = Range("B2")
LastRow = Cells(Rows.Count, StartCell.Column).End(xlUp).Row
Set rng = Range(StartCell, Cells(LastRow, 2))
For Each cl In rng.SpecialCells(xlCellTypeVisible)
MsgBox cl
Next cl
End Sub
上面的代码可以很好地处理其余的过滤数据,例如“IH”、“II”和“VO”,因为隐藏的行介于两者之间。与“IG”不同,其余隐藏行位于其下方。
当我首先尝试过滤“IG”时,它只显示值“reference”、“count”、“code”并且永远不会停止循环。
我将如何调整我的代码以适应这种情况,因为我很可能会在未来的数据中遇到同样的情况。
【问题讨论】: