Probably a bug when using ListObject with Autofilter

pistachia

New Member
Joined
Feb 11, 2016
Messages
2
Hi,

I created a workbook which has 2 worksheets. Sheet1 contains a Students ExcelTable. Sheet2 contains a Teachers ExcelTable. I am trying to filter records in the Students Table using the following macro. When running the macro with Sheet1 active, the filtered range, rng2 is $A$1:$B$2 as expected. However with Sheet2 active, rng2 becomes $A$1:$B$2,$A$4:$B$4, two areas. This only happens when the Teachers Table contains more records than the Students Table. What am I missing or is this a bug? Any ideas. Thanks.

Frank

Sheet1: Students Table ($A$1:$B$3):
Name Class
Alice 1A
Bob 1B

Sheet2: Teachers Table ($A$1:$B$4):
Name Class
Dorothe 2A
Eddy 1A
Florence 2B

Code:
Sub StudentTableTest()
Dim loStudents As ListObject
Dim rng2 As Range

    Set loStudents = Sheets("Students").ListObjects("Students")
    
    loStudents.Range.AutoFilter field:=loStudents.ListColumns("Name").Index, Criteria1:="Alice"
    loStudents.Range.AutoFilter field:=loStudents.ListColumns("Class").Index, Criteria1:="1A"
    Set rng2 = loStudents.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    
    MsgBox "rng2:" & vbCrLf & _
        "      .Worksheet.Name=" & rng2.Worksheet.Name & vbCrLf & _
        "      .Address=" & rng2.Address & vbCrLf & _
        "      .Areas.Count=" & rng2.Areas.Count
End Sub

The workbook can be downloaded here
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Sorry that the code in above post should be corrected with the Sheets("Sheet1") instead of Sheets("Students") as below.
Code:
Sub StudentTableTest()
Dim loStudents As ListObject
Dim rng2 As Range

    Set loStudents = Sheets("Sheet1").ListObjects("Students")
    
    loStudents.Range.AutoFilter field:=loStudents.ListColumns("Name").Index, Criteria1:="Alice"
    loStudents.Range.AutoFilter field:=loStudents.ListColumns("Class").Index, Criteria1:="1A"
    Set rng2 = loStudents.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    
    MsgBox "rng2:" & vbCrLf & _
        "      .Worksheet.Name=" & rng2.Worksheet.Name & vbCrLf & _
        "      .Address=" & rng2.Address & vbCrLf & _
        "      .Areas.Count=" & rng2.Areas.Count
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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