Copy Resized Range of Filtered Data in VBA

DenialDan

New Member
Joined
May 31, 2017
Messages
15
Hello,

As the title suggests I'm trying to copy a range of filtered cells. The source has a lot more than 5 rows but of these filtered cells I'm only trying to copy the first 5. This is the code I've been using so far:

Code:
Sub Top5()


Dim CopyRange As Range


Sheets(5).Range("B3").AutoFilter Field:=1, Criteria1:="2017-Q3"
Sheets(7).Range("B3").AutoFilter Field:=1, Criteria1:="2017-Q3"


On Error Resume Next
With Sheets(5).AutoFilter.Range


    Set CopyRange = .Offset(1, 0).Resize(5).SpecialCells(xlCellTypeVisible)


End With


CopyRange.Copy Sheets("FC").Range("C30:C34") 


End Sub

I've added the resize property for ensuring it's not longer than 5 rows and the offset property so the header is excluded.
My problem is that this code copies the whole table but I only want to copy the first five in column "B" and only 3 rows are copied.

Best Regards,
DenialDan.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try...

Code:
[FONT=Arial][COLOR=darkblue]Sub[/COLOR] Top5()

    [COLOR=darkblue]Dim[/COLOR] FiltRange [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] CopyRange [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] Rw [COLOR=darkblue]As[/COLOR] Range
    [COLOR=darkblue]Dim[/COLOR] RwCnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    Sheets("FC").Range("C30").CurrentRegion.ClearContents [COLOR=green]'optional[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] Sheets(5)
        [COLOR=darkblue]If[/COLOR] .FilterMode [COLOR=darkblue]Then[/COLOR] .ShowAllData
        .Range("B3").AutoFilter Field:=1, Criteria1:="2017-Q3"
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
    [COLOR=darkblue]With[/COLOR] Sheets(5).AutoFilter.Range
        [COLOR=darkblue]Set[/COLOR] FiltRange = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
    
    [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] FiltRange [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
        RwCnt = 0
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] Rw [COLOR=darkblue]In[/COLOR] FiltRange.Rows
            RwCnt = RwCnt + 1
            [COLOR=darkblue]If[/COLOR] CopyRange [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
                [COLOR=darkblue]Set[/COLOR] CopyRange = Rw
            [COLOR=darkblue]Else[/COLOR]
                [COLOR=darkblue]Set[/COLOR] CopyRange = Union(CopyRange, Rw)
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]If[/COLOR] RwCnt = 5 [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
        [COLOR=darkblue]Next[/COLOR]
        CopyRange.Copy Sheets("FC").Range("C30")
        Sheets("FC").Activate [COLOR=green]'optional[/COLOR]
     [COLOR=darkblue]Else[/COLOR]
        Sheets("FC").Activate [COLOR=green]'optional[/COLOR]
        MsgBox "No records found.", vbInformation
     [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
     
     Sheets(5).AutoFilter.ShowAllData [COLOR=green]'optional[/COLOR]

[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Hope this helps!
 
Upvote 0
Thanks for the reply Domenic. Sadly when I implemented the code im getting a run-time error '1004': This action won't work on multiple selections. The debugger point to the line:

Code:
CopyRange.Copy Sheets("FC").Range("C30")[/FONT][/COLOR][COLOR=#333333][FONT=Arial]

 
Upvote 0
Did you make any changes to the code? If so, can you post the exact code that you're using?
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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