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
The workbook can be downloaded here
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