VBA Autofilter Cell(3).Value2

GlennL

New Member
Joined
Sep 25, 2018
Messages
37
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello,

I am trying to get the value2 of the third row of column6 of a worksheet which has an autofilter applied.

I tried the code below, but I am getting the value of as if there is no autofilter.

Code:
Set rng = ws.AutoFilter.Range.columns(6).SpecialCells(xlCellTypeVisible).Cells(3)

Any suggestions?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I hope I've understood your question correctly. If so you can put your visible cells into an array and then do with them what you will.

For example, with the following sheet:

Book1
ABCDEF
1YellowGreenBluePinkBlackWhite
21
31
4ABCDE1
51
61
71
81
91
101
111
121
131
141
152
162
17ZYXWV2
182
192
202
Sheet1


If you autofilter this range and filter by column White=1, the code below returns A,B,C,D,E,1
If you filter by White=2, code returns Z,Y,X,W,V,2.

VBA Code:
Sub test()
    Dim i As Long
    Dim arr As Variant
    arr = Range("a2:f20").SpecialCells(xlCellTypeVisible).Value2
    For i = LBound(arr, 2) To UBound(arr, 2)
        Debug.Print arr(3, i)
    Next i
End Sub
 
Upvote 0
That code will only return the 1st visible row, whereas the OP is trying to get the value of the 3rd visible row.
 
Upvote 0
Assigning the Value property to an array only works for contiguous ranges. If you use specialcells, you will only get the values from the first visible area.
 
Upvote 0
Assigning the Value property to an array only works for contiguous ranges. If you use specialcells, you will only get the values from the first visible area.
I don't understand your explanation Rory. You guys are better at this than me, but my interpretation was OP wanted values from third visible row.

Then if I have this:
1719486308557.png


Code returns A, B, C, D, E, 1

and if I have this:

1719486377607.png


code returns Z, Y, X, W, V, 2.

Is that not what was asked for?
 
Upvote 0
Your code only works because the column you are filtering on happens to have all the same values in contiguous blocks. Change F3 to 2, reapply the filter for 1, then try your code.
 
Upvote 0
This is a bit longwinded but should work.
Might need some additional checks for if there are less than 3 cells visible.

VBA Code:
Sub GetOccurenceOfVisibleCells()

Dim ws As Worksheet
Dim rngVis As Range
Dim rArea As Range
Dim rCell As Range
Dim iCell As Long, iTgtCell As Long
Dim rTgt As Range

Set ws = ActiveSheet

Set rngVis = ws.AutoFilter.Range.Columns(6).Offset(1).SpecialCells(xlCellTypeVisible)

iTgtCell = 3

For Each rArea In rngVis.Areas
    For Each rCell In rArea
        iCell = iCell + 1
        If iCell = iTgtCell Then
            Set rTgt = rCell
            Exit For
        End If
    Next rCell
    If iCell = iTgtCell Then Exit For
Next rArea

Debug.Print rTgt.Address, rTgt.Value

End Sub
 
Upvote 0
You don't need to loop areas and then cells with a for each loop - just the Cells will do.
 
Upvote 0
Here's another option you could try (I don't know if there's a more direct way).

VBA Code:
Sub M()
Dim a As Range, rng As Range, RowNum As Long
RowNum = 3 'Row to find
For Each a In Columns(6).SpecialCells(xlCellTypeVisible).Areas
    If a.Rows.Count >= RowNum Then
        Set rng = a.Cells(RowNum)
        Exit For
    Else
        RowNum = RowNum - a.Rows.Count
    End If
Next a
MsgBox rng.Address
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,128
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top