VBA to copy Filtered top 10 data to another sheet

Turbo68

Board Regular
Joined
Jan 31, 2014
Messages
118
I have this started with doing some google searches and the below code works but it is copying the wrong col in my original:

'Copy Sorted TOP 10

Dim i As Long
Dim r As Range
Dim rWC As Range

Set r = Range("D16", Range("D" & Rows.Count).End(xlUp)).SpecialCells(10)

For Each rWC In r
i = i + 1
If i = 10 Or i = r.Count Then Exit For
Next rWC
Range(r(1), rWC).Resize(, 10).SpecialCells(10).Copy


Sheets("Division TOP-BOTTOM 10").Select
Range("TOP_10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

My Data looks like this:

1588651362977.png


As you can see the A is St#, B is Area, C is blank, D is the COL i want it to highlight the top 10 entries. Right now the code i am using is highlighing this:

1588651409838.png


It is not selecting the visiable cells but it is selecting 10 cells but selecting A and B col and not D either! I thought i had r= row D in the setup but not a clue why its not selcting correctly.

Major confusion here... :)

Any help is much appreciated.

Mike
 

Attachments

  • 1588650923567.png
    1588650923567.png
    28.4 KB · Views: 3
  • 1588651074256.png
    1588651074256.png
    27.2 KB · Views: 1
'Copy Sorted TOP 10

Dim i As Long
Dim r As Range
Dim rWC As Range

Set r = Range("D16", Range("D" & Row.Count).End(xlUp)).SpecialCells(10)

For Each rWC In r
i = i + 1
If i = 10 Or i = r.Count Then Exit For
Next rWC
Range(r(1), rWC).Resize(, 12).SpecialCells(12).Copy


Sheets("Division TOP-BOTTOM 10").Select
Range("TOP_10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
It should be
VBA Code:
Dim i As Long
Dim r As Range
Dim rWC As Range

Set r = Range("D16", Range("D" & Rows.count).End(xlUp)).SpecialCells(12)

For Each rWC In r
i = i + 1
If i = 10 Or i = r.count Then Exit For
Next rWC
Range(r(1), rWC).Resize(, 10).SpecialCells(12).Select


Sheets("Division TOP-BOTTOM 10").Select
Range("TOP_10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
& if you only want column D copied, then it should be
VBA Code:
Dim i As Long
Dim r As Range
Dim rWC As Range

Set r = Range("D16", Range("D" & Rows.count).End(xlUp)).SpecialCells(12)

For Each rWC In r
i = i + 1
If i = 10 Or i = r.count Then Exit For
Next rWC
Range(r(1), rWC).SpecialCells(12).Select


Sheets("Division TOP-BOTTOM 10").Select
Range("TOP_10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
 
Upvote 0
Yes, I was just writing to that effect. Changed that one 12 to 1 and got my results!!!

Thanks, Fluff for the assistance!

Mike
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Well, got one more question... I am going to run that code twice in one SUB but I think I need to reset the DIM I as Long, Dim r as Range, and Dim rWC as Range? It copies a lot more the 2nd time it runs that code. Is there a way to reset those without re-running another sub coding?

M
 
Upvote 0
Just put i=0 before you start the next loop & you should be ok.
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,306
Members
449,218
Latest member
Excel Master

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