【问题标题】:How to get value of visible row data in specialCell(xlCellTypeVisible)如何在 specialCells(xlCellTypeVisible) 中获取可见行数据的值
【发布时间】: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”并且永远不会停止循环。

我将如何调整我的代码以适应这种情况,因为我很可能会在未来的数据中遇到同样的情况。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以从B1 开始您的范围并跳过第一行将打印。

    在这里试试这个:

    Sub Try()
    
    Dim cl As Range, rng As Range
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim rw As row
    
    Dim StartCell As Range
    Set StartCell = Range("B1")
    
    LastRow = Cells(Rows.count, StartCell.Column).End(xlUp).row
    
    Set rng = Range(StartCell, Cells(LastRow, 2))
    
    For Each cl In rng.SpecialCells(xlCellTypeVisible)
    
        If Not cl.row = 1 Then MsgBox cl
    
    Next cl
    
    
    End Sub
    

    【讨论】:

    • 谢谢米库!从来没有想过'如果不是',它会解决我的问题。
    猜你喜欢
    • 2017-04-01
    • 2023-02-25
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多