coutning visible rows with filter

Posted on 2011-09-16
Last Modified: 2012-05-12
Hi guys,

Going crazy here,  I'm starting with a large excel data sheet.
Sheet attached...  Then for a array I have with account numbers... I use the filter for each account.   So it only displays the 1 accounts information on the screen.

Then I count the visible rows using
            counter = Selection.Columns(1).SpecialCells(xlCellTypeVisible).Rows.Count
                If counter > 1 Then

The weird thing is the first account I filter... it works accurately.  So for example, if I look at 1st account 05a-000016, it shows 2 rows... the header, and the information.
And the counter variable = 2 as it should

Then when I go to 2nd account, 05a-000091, it accurately filters information so I am now viewing 3 rows, the header and 2 lines of info.  However, using the same code, the counter states 1.

Any ideas why the counter works the 1st time, then the second time counter goes to 1?

thanks in advance!!!
Sub CreateGroupEmailCF()
Dim x As Integer
Dim counter As Integer
        ActiveWorkbook.SaveAs Filename:= _
                    "I:\Macro\Coffee\Recap_Email.xlsx", FileFormat:= _
                    xlOpenXMLWorkbook, CreateBackup:=False
        For x = 1 To UBound(AcctListCF)
            With ActiveSheet
                .AutoFilterMode = False
                With .Range("A1:S1")
                    .AutoFilter Field:=5, Criteria1:=AcctListCF(x)
                End With
            End With
            counter = Selection.Columns(1).SpecialCells(xlCellTypeVisible).Rows.Count
                If counter > 1 Then
                    Selection.Copy Destination:=Workbooks("Recap_Email.xlsx").Sheets(1).Range("A65536").End(xlUp).Offset(1, 0)
                End If
        Next x
        Call FormatSheet

End Sub

Open in new window

Question by:solarissf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2

Expert Comment

ID: 36551275
Could you not simply create a pivot table and read you values from that?

Author Comment

ID: 36551777
for formatting reasons I need to keep the layout in its original form
LVL 17

Expert Comment

ID: 36552002
The reason is that SpecialCells is returning a range that has multiple areas, and Rows.Count is only counting the cells in the first area, i.e. the first contiguous range of cells.  You will need to loop through each Area in the selection and sum the counts for each. Something like this (not tested as I don't have all your code, but you can see the principle):

            Dim a As Excel.Range
            counter = 0
            For Each a In Selection.Columns(1).SpecialCells(xlCellTypeVisible)
                counter = counter + a.Rows.Count
            Next a

Open in new window

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 17

Expert Comment

ID: 36552052
Looking again, I suppose if Selection.Columns(1).SpecialCells(xlCellTypeVisible).Areas.Count > 1 then you know you need to do your copy as there must be at least 2 rows - it's only if there's one area that you need the count, if I'm reading your code right
LVL 17

Expert Comment

ID: 36552150
Correction - line 3 should (crucially) read

            For Each a In Selection.Columns(1).SpecialCells(xlCellTypeVisible).Areas

Open in new window

LVL 27

Accepted Solution

Glenn Ray earned 250 total points
ID: 36553200
A much simpler method would be to use
WorksheetFunction.COUNTIF(selection, AcctListCF(x))

Here is code that would replace lines 18-20 in your original post:
counter = WorksheetFunction.CountIf(Selection, AcctListCF(x))

Open in new window

All other behavior is preserved.
LVL 27

Expert Comment

by:Glenn Ray
ID: 36553226 my haste to post...missed the selection logic.  This code replaces lines 18-20
If AcctListCF(x) = "" Then
    counter = 0
    counter = WorksheetFunction.CountIf(Selection, AcctListCF(x))
End If

Open in new window

LVL 17

Assisted Solution

andrewssd3 earned 250 total points
ID: 36553595
Agreed COUNTIF would probably be a simpler solution - I was just answering the question!  Having said that, you would need some more logic to do the selection so the rows could be copied as they are in Line 22, which would mean running through each row and adding it to the selection using Application.Union if it matched.  I personally would probably still prefer to do that rather than using filter, but I don't think there's very much in it either way.
LVL 85

Expert Comment

by:Rory Archibald
ID: 36559869
Since you are only looking at one column, just use:

counter = Selection.Columns(1).SpecialCells(xlCellTypeVisible).Count

which will work with discrete areas.

Author Comment

ID: 36560864
ended up using countif... much easier solution I didnt think about before

thanks for all the help everyone!

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question