Selecting 1st 10 Rows of visible Cells in VBA Filtered Table

徘徊边缘 提交于 2021-02-08 08:16:56

问题


New to Coding & VBA and this is my first attempt at trying to Select and Copy the 1st 10 rows of visible data together with the table Header in a Table that I have filtered using VBA Macro. I used examples of code in this link on stackoverflow. VBA selecting visible cells after filtering. This particular example allows me to learn how to copy the values in a single column. I would like to copy the values for the entire Row, or some of the columns depending on which is easier.

Sub LPRDATA()


Dim TYEAR    As String
TYEAR = Range("TYEAR").Value
Dim QUARTER    As String
QUARTER = Range("QUARTER").Value
Dim r As Range, rC As Range
Dim j As Long


Sheets("CONTROL").Select
Sheets("DATA").Visible = True
Sheets("CONTROL").Select
Sheets("10").Visible = True
Sheets("DATA").Select
    ActiveWorkbook.Worksheets("DATA").ListObjects("tblData").Sort.SortFields.Clear
     ActiveWorkbook.RefreshAll
     Range("tblData[[#Headers],[Sn '#]]").Select

Range("tblData[[#Headers],[Year]]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("tblData").Range.AutoFilter Field:=2, Criteria1:="LPR"
ActiveSheet.ListObjects("tblData").Range.AutoFilter Field:=12, Criteria1:=TYEAR
ActiveSheet.ListObjects("tblData").Range.AutoFilter Field:=15, Criteria1:=QUARTER


ActiveWorkbook.Worksheets("DATA").ListObjects("tblData").Sort.SortFields.Add _
    Key:=Range("tblData[[#All],[Score]]"), SortOn:=xlSortOnValues, Order:= _
    xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("DATA").ListObjects("tblData").Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Set r = Nothing
Set rC = Nothing
j = 0
Set r = Range("C1", Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)

For Each rC In r
j = j + 1
If j = 11 Or j = r.Count Then Exit For
Next rC

Range(r(1), rC).SpecialCells(xlCellTypeVisible).Copy
Sheets("10").Select
Range("C7").Select
ActiveSheet.Paste


End Sub

回答1:


I've stripped away some of your code, to make this example easier to follow.

' Sorts a table in Excel.
' Then filters table for first 10 records.
' Then copies/pastes.
Sub Example()
    Dim sourceWS As Worksheet       ' Spreadsheet that contains the table.
    Dim sourceLO As ListObject      ' Table that contains the data.
    Dim targetRange As Range        ' Where to copy data to.

    ' Populate vars.
    Set sourceWS = ActiveSheet
    Set sourceLO = sourceWS.ListObjects("tblData")
    Set targetRange = Range("10!C7")

    ' Sort the table.
    With sourceLO.Sort
        .SortFields.Add Range("tblData[[#All], [Score]]"), xlSortOnValues, xlDescending
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    ' Limit to first ten rows (update field to select limiting column).
    sourceLO.Range.AutoFilter Field:=1, Criteria1:="10", Operator:=xlTop10Items

    ' Copy currenlty visible cells.
    sourceLO.Range.SpecialCells(xlCellTypeVisible).Copy
    targetRange.PasteSpecial (xlPasteAll)
End Sub

The line starting sourceLO.Range.AutoFilter... limits the table to the first 10 recrods.

The line starting sourceLO.Range.SpecialCells... copies only the visible rows (i.e. the first 10). If you want to limit the copy/paste to certain columns use this syntax:

Range("tblData[Header 1], tblData[Header 2]").SpecialCells(xlCellTypeVisible).Copy



回答2:


You could consider instead using a loop to go through row by row and find the first ten rows that match your criteria, without using the filter.

Unless you need the filter to visually display the data to the user in a particular way, a loop is potentially more flexible and easier to code.

You could also consider storing your values in variables instead of using copy-paste - personally I find it a bit of an annoyance when a macro wipes my clipboard :)

I normally use this page to refer back to for the syntax for loops in vba: http://www.excel-easy.com/vba/loop.html



来源:https://stackoverflow.com/questions/48402803/selecting-1st-10-rows-of-visible-cells-in-vba-filtered-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!