Solved

Counting the total visible rows that an autofilter produces

Posted on 2014-02-25
17
274 Views
Last Modified: 2014-02-26
The following code needs to place the total visible rows produced by an autofilter to a specific spot on another sheet tab.  How would I modify the code to do this?

Sub Macro2()
'
    Sheets("CDPSRECRPT").Select
    Rows("1:1").Select
    Selection.AutoFilter
    ActiveWindow.LargeScroll ToRight:=1
    ActiveSheet.UsedRange.AutoFilter Field:=16, Operator:= _
        xlFilterValues, Criteria2:=Array(2, DateValue(Now))
'Need to count how many visible cells the autofilter produced.

    Sheets("MOT-MeasureData").Select
    Rows("2:2").Select 'insert a row under headers.
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Rows("2:2").Select  'fix row height
    Selection.RowHeight = 14.4
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("A2").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("B2").Select
    
'This is where I need to insert the value from above, total visible cells
'from autofilter

End Sub

Open in new window

0
Comment
Question by:RWayneH
  • 10
  • 6
17 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39885688
Hi,

pls try

    RowCnt = ActiveSheet.UsedRange.Columns(1).SpecialCells(xlVisible).Count

Open in new window

Regards
0
 

Author Comment

by:RWayneH
ID: 39885712
So how does this value get placed in the other sheet tab?  Can you insert the needed code in the example?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39885778
RWayneH,

Keep in mind that SpecialCells can be unreliable when there are more than about 16,000 rows in your source data.  (The actual bug is more than 8192 discrete regions, for which you would need at least 16,384 rows.)

Can this be the case for you, or will you always be safely under that limit?

Patrick
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39885804
So I suppose you mean

Sub Macro2()
'
    Sheets("CDPSRECRPT").Select
    Rows("1:1").Select
    Selection.AutoFilter
    ActiveWindow.LargeScroll ToRight:=1
    ActiveSheet.UsedRange.AutoFilter Field:=16, Operator:= _
        xlFilterValues, Criteria2:=Array(2, DateValue(Now))
'Need to count how many visible cells the autofilter produced.
    RowCnt = ActiveSheet.UsedRange.Columns(1).SpecialCells(xlVisible).Count
    Sheets("MOT-MeasureData").Select
    Rows("2:2").Select 'insert a row under headers.
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Rows("2:2").Select  'fix row height
    Selection.RowHeight = 14.4
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("A2").Select
    Selection.NumberFormat = "m/d/yyyy"
    Range("B2").Select
    Range("B2") = RowCnt
'This is where I need to insert the value from above, total visible cells
'from autofilter

End Sub

Open in new window

REgards
0
 

Author Comment

by:RWayneH
ID: 39885915
Yes exactly what I need, I am testing now... and the number of rows will only be a couple thousand rows. Thanks.
0
 

Author Comment

by:RWayneH
ID: 39886162
I would like to add an extension to this question.  How would I select and copy all the visible cells in column A? with the same autofilter on?  It gets the counts great, thanks, but I forgot that I need to select and copy column A too.
0
 

Author Comment

by:RWayneH
ID: 39886175
ending the macro on sheet CDPSRECRPT, with visible cells in column A selected and copied into the clipboard
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39886192
Pls try

Sheets("CDPSRECRPT").Select
Sheets("CDPSRECRPT").UsedRange.Columns(1).SpecialCells(xlVisible).Copy

Open in new window

Regards
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:RWayneH
ID: 39886194
What is Ln2 telling it?
0
 

Author Comment

by:RWayneH
ID: 39886203
Is it putting them in the MOT-MeasureData sheet at cell B4?  I am pasting the list into SAP so I do not want to placed anywhere accept into the clipboad.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39886223
On my Edited Version

Sheets("CDPSRECRPT").Select
Sheets("CDPSRECRPT").UsedRange.Columns(1).SpecialCells(xlVisible).Copy 

Open in new window


It only copies the data into the clipboard
0
 

Author Comment

by:RWayneH
ID: 39886318
Sorry but it is grabbing the headers... how do I tell to grab everything but my header?
0
 

Author Comment

by:RWayneH
ID: 39887574
The headers cannot be part of the copy.. it is using the header and failing.  How do I tell the copy to exclude the header row.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39887968
pls try


Sheets("CDPSRECRPT").Select
Set UsdRng = Sheets("CDPSRECRPT").UsedRange
Set myRng = UsdRng.Offset(1).Resize(UsdRng.Rows.Count - 1, UsdRng.Columns.Count)
myRng.Columns(1).SpecialCells(xlVisible).Copy

Open in new window

Regards
0
 

Author Comment

by:RWayneH
ID: 39888405
Ok thanks..  does the count use the header row too?  Does that need to chg'd also?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39888439
That's right

so use instead
RowCnt = ActiveSheet.UsedRange.Columns(1).SpecialCells(xlVisible).Count -1

Open in new window

   
Regards
0
 

Author Closing Comment

by:RWayneH
ID: 39889247
EXCELlent!! -R-
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now