简体   繁体   English

使用 VBA 将过滤后的数据复制到另一个工作表

[英]Copy filtered data to another sheet using VBA

I have two sheets.我有两张床单。 One has the complete data and the other is based on the filter applied on the first sheet.一个具有完整的数据,另一个基于应用于第一张纸的过滤器。

Name of the data sheet : Data数据表名称: Data
Name of the filtered Sheet : Hoky过滤表的名称: Hoky

I am just taking a small portion of data for simplicity.为简单起见,我只取了一小部分数据。 MY objective is to copy the data from Data Sheet, based on the filter.我的目标是根据过滤器从数据表中复制数据。 I have a macro which somehow works but its hard-coded and is a recorded macro.我有一个宏,它以某种方式工作,但它是硬编码的,是一个录制的宏。

My problems are:我的问题是:

  1. The number of rows is different everytime.行数每次都不一样。 (manual effort) (人工努力)
  2. Columns are not in order.列不按顺序。

在此处输入图片说明 在此处输入图片说明

Sub TESTTHIS()
'
' TESTTHIS Macro
'
'FILTER
Range("F2").Select
Selection.AutoFilter
ActiveSheet.Range("$B$2:$F$12").AutoFilter Field:=5, Criteria1:="hockey"

'Data Selection and Copy
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Hockey").Select
Range("E3").Select
ActiveSheet.Paste

Sheets("Data").Select
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("D3").Select
ActiveSheet.Paste

Sheets("Data").Select
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Hockey").Select
Range("C3").Select
ActiveSheet.Paste

End Sub

Best way of doing it最好的方法

Below code is to copy the visible data in DBExtract sheet, and paste it into duplicateRecords sheet, with only filtered values.下面的代码是复制DBExtract表中的可见数据,粘贴到duplicateRecords表中,只有过滤值。 Range selected by me is the maximum range that can be occupied by my data.我选择的范围是我的数据可以占用的最大范围。 You can change it as per your need.您可以根据需要更改它。

  Sub selectVisibleRange()

    Dim DbExtract, DuplicateRecords As Worksheet
    Set DbExtract = ThisWorkbook.Sheets("Export Worksheet")
    Set DuplicateRecords = ThisWorkbook.Sheets("DuplicateRecords")

    DbExtract.Range("A1:BF9999").SpecialCells(xlCellTypeVisible).Copy
    DuplicateRecords.Cells(1, 1).PasteSpecial


    End Sub

When i need to copy data from filtered table i use range.SpecialCells(xlCellTypeVisible).copy.当我需要从过滤表中复制数据时,我使用 range.SpecialCells(xlCellTypeVisible).copy。 Where the range is range of all data (without a filter).其中范围是所有数据的范围(没有过滤器)。

Example:示例:

Sub copy()
     'source worksheet
     dim ws as Worksheet
     set ws = Application.Worksheets("Data")' set you source worksheet here
     dim data_end_row_number as Integer
     data_end_row_number = ws.Range("B3").End(XlDown).Row.Number
    'enable filter
    ws.Range("B2:F2").AutoFilter Field:=2, Criteria1:="hockey", VisibleDropDown:=True
    ws.Range("B3:F" & data_end_row_number).SpecialCells(xlCellTypeVisible).Copy
    Application.Worksheets("Hoky").Range("B3").Paste
    'You have to add headers to Hoky worksheet
end sub

I suggest you do it a different way.我建议你换一种方式。

In the following code I set as a Range the column with the sports name F and loop through each cell of it, check if it is "hockey" and if yes I insert the values in the other sheet one by one, by using Offset .在下面的代码中,我将带有运动名称 F 的列设置为Range循环遍历其中的每个单元格,检查它是否是“曲棍球”,如果是,则使用Offset将值一个接一个地插入到另一个工作表中。

I do not think it is very complicated and even if you are just learning VBA, you should probably be able to understand every step.我不认为它很复杂,即使您只是学习 VBA,您也应该能够理解每一步。 Please let me know if you need some clarification如果您需要澄清,请告诉我

Sub TestThat()

'Declare the variables
Dim DataSh As Worksheet
Dim HokySh As Worksheet
Dim SportsRange As Range
Dim rCell As Range
Dim i As Long

'Set the variables
Set DataSh = ThisWorkbook.Sheets("Data")
Set HokySh = ThisWorkbook.Sheets("Hoky")

Set SportsRange = DataSh.Range(DataSh.Cells(3, 6), DataSh.Cells(Rows.Count, 6).End(xlUp))
    'I went from the cell row3/column6 (or F3) and go down until the last non empty cell

    i = 2

    For Each rCell In SportsRange 'loop through each cell in the range

        If rCell = "hockey" Then 'check if the cell is equal to "hockey"

            i = i + 1                                'Row number (+1 everytime I found another "hockey")
            HokySh.Cells(i, 2) = i - 2               'S No.
            HokySh.Cells(i, 3) = rCell.Offset(0, -1) 'School
            HokySh.Cells(i, 4) = rCell.Offset(0, -2) 'Background
            HokySh.Cells(i, 5) = rCell.Offset(0, -3) 'Age

        End If

    Next rCell

End Sub

it needs to be .Row.count not Row.Number?它需要是 .Row.count 而不是 Row.Number?

That's what I used and it works fine Sub TransfersToCleared() Dim ws As Worksheet Dim LastRow As Long Set ws = Application.Worksheets("Export (2)") 'Data Source LastRow = Range("A" & Rows.Count).End(xlUp).Row ws.Range("A2:AB" & LastRow).SpecialCells(xlCellTypeVisible).Copy这就是我使用的,它工作正常 Sub TransfersToCleared() Dim ws As Worksheet Dim LastRow As Long Set ws = Application.Worksheets("Export (2)") 'Data Source LastRow = Range("A" & Rows.Count)。 End(xlUp).Row ws.Range("A2:AB" & LastRow).SpecialCells(xlCellTypeVisible).Copy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM