在VBA筛选表中选择可见单元格的第10行 [英] Selecting 1st 10 Rows of visible Cells in VBA Filtered Table
问题描述
编码&的新知识VBA,这是我第一次尝试选择并复制前10行可见数据以及我使用VBA宏过滤的表中的表 Header .我在stackoverflow的此链接中使用了代码示例. VBA过滤后选择可见单元.这个特定的示例使我学习了如何将值复制到单个列中.我想复制整个行"或某些列的值,具体取决于哪一个更容易.
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
推荐答案
为了使本示例更容易理解,我剥离了一些代码.
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
以 sourceLO.Range.AutoFilter ...
开始的行将表格限制在前10个后退位置.
The line starting sourceLO.Range.AutoFilter...
limits the table to the first 10 recrods.
以 sourceLO.Range.SpecialCells ...
开始的行仅复制可见行(即前10行).如果要将复制/粘贴限制为某些列,请使用以下语法:
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
这篇关于在VBA筛选表中选择可见单元格的第10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!