在VBA过滤表中选择第10行可见单元格

时间:2018-01-23 13:22:36

标签: excel vba excel-vba filtering extract

Coding& amp; VBA,这是我尝试选择并复制前10行可见数据以及表格标题在我使用VBA宏过滤的表格中的第一次尝试。我在stackoverflow上使用了此链接中的代码示例。 VBA selecting visible cells after filtering。这个特定的例子让我学习如何在单个列中复制值。我想复制整个Row的值,或者根据哪些列更容易复制一些列。

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

2 个答案:

答案 0 :(得分:1)

我已经删除了一些代码,以便让这个示例更容易理解。

' 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

sourceLO.Range.AutoFilter...开始的行将表格限制为前10个记录。

sourceLO.Range.SpecialCells...开始的行仅复制可见行(即前10行)。如果要将复制/粘贴限制为某些列,请使用以下语法:

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

答案 1 :(得分:0)

您可以考虑使用循环逐行查找并找到符合条件的前十行,而不使用过滤器。

除非您需要过滤器以特定方式以可视方式向用户显示数据,否则循环可能更灵活,更易于编码。

您还可以考虑将值存储在变量中而不是使用复制粘贴 - 我个人觉得当宏擦除我的剪贴板时会有点烦恼:)

我通常使用此页面来引用vba中的循环语法: http://www.excel-easy.com/vba/loop.html