Autofilter and Count Row to insert on another worksheet

ElianaMG

New Member
Joined
Feb 19, 2015
Messages
3
I am working on two worksheet in the same workbook, wsSheet1 and wsSheet2, and I need to do an autofilter (unique = true) in wsSheet1, count the lines and insert one line for each value on wsSheet2, but I just can't get it done! So far my code is not filtering only cells with values, so I can see some blank lines on my range, CopyToRange simply doesn't work and it is counting all lines, not just the visible ones. I am using Office 2010. Could you please help?

Code:
sub AutofilterToCopy

Dim wsSheet1 As Worksheet
Dim wsSheet2 As Worksheet
Dim rngCol As Long
Dim rngCount As Integer

    Set wsSheet1 = Sheet1
    Set wsSheet2 = Sheet2


wsSheet1.Select

[COLOR=#008000]'This Autofilter don't work[/COLOR]
    wsSheet1.Range("Q10:Q45").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=( _
        wsSheet2.Range("R10")), Unique:=True

 [COLOR=#008000]'This CountIF don't work[/COLOR]
[indent]rngCol = wsSheet1.Range("Q" & MaxRows).End(xlUp).Row[/indent]
[indent]rngCount = WorksheetFunction.CountIf(Range("Q10:Q" & rngCol), "<>""")[/indent]


If wsLine.FilterMode = True Then
    wsLine.ShowAllData
End If
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
See if this modified version will work.
Code:
Sub AutofilterToCopy()
Dim wsSheet1 As Worksheet
Dim wsSheet2 As Worksheet
Dim rngCol As Long
Dim rngCount As Integer
    Set wsSheet1 = Sheets("Sheet1")
    Set wsSheet2 = Sheets("Sheet2")
wsSheet1.Select
'This Autofilter don't work
    wsSheet1.Range("Q10:Q45").AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=wsSheet2.Range("R10"), Unique:=True
 'This CountIF don't work
rngCol = wsSheet1.Range("Q" & Rows.Count).End(xlUp).Row
rngCount = Application.CountIf(wsSheet1.Range("Q10:Q" & rngCol), "<>""")
If wsLine.FilterMode = True Then
    wsLine.ShowAllData
End If
 
Upvote 0
Hi JLGWhiz, thanks for the responde, but this part below is not counting every row in the range not only the visible ones, any idea?

See if this modified version will work.
Code:
rngCol = wsSheet1.Range("Q" & Rows.Count).End(xlUp).Row
rngCount = Application.CountIf(wsSheet1.Range("Q10:Q" & rngCol), "<>""")
 
Upvote 0
Hi JLGWhiz, thanks for the responde, but this part below is not counting every row in the range not only the visible ones, any idea?

Too many quote marks. Try this:
Code:
rngCount = Application.CountIf(wsSheet1.Range("Q10:Q" & rngCol), "<>")
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,125,000
Members
449,202
Latest member
Pertotal

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