0

我有一个复杂的问题...我已将 .csv 文件中的数据导入工作表(导入数据)。现在在另一个工作表(导入函数)中,我使用一个按钮作为宏来过滤掉某些数据。过滤器取决于在单元格 A2、单元格 B2、单元格 C2 处填写的数据(这些过滤器由 excel 用户填写)。如果这些过滤器与工作表(导入的数据)中的一行匹配,则应将这些值粘贴到另一个工作表上。我该怎么做呢?A2、B2 和 C2 中的这些过滤器与其他工作表中的 A、B、C 匹配

提前谢谢..

作为回复,我现在有代码:

Sub FilterButton()
If MsgBox("Are you sure the fields 'Collection' and 'System' are filled in?", vbInformation + vbYesNo, "Sort function") = vbYes Then
    'If vbYesNo = Yes Then'

Dim ws1, ws2, ws3 As Worksheet
Dim filter1, filter2, filter3 As String
Dim lrow As Double

Set ws1 = ThisWorkbook.Sheets("Import") 'this contains the filters
Set ws2 = ThisWorkbook.Sheets("Imported Data") 'this contains the text file
Set ws3 = ThisWorkbook.Sheets("Test") ' this is the destination

With ws1
    filter1 = "=" & .Range("A2").Value
    filter2 = "=" & .Range("B2").Value
    filter3 = "=" & .Range("C2").Value
End With

With ws2
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row

With .Range("A1:G" & lrow)
        .AutoFilter Field:=1, Criteria1:=filter1
        .AutoFilter Field:=2, Criteria1:=filter2
        .AutoFilter Field:=3, Criteria1:=filter3
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
        ws3.Cells(.Range("A" & .Rows.Count).End(xlUp).Row + 1, 1)
End With

.AutoFilterMode = False

End With

End If

End Sub

我的搜索条件(来自 ws1 的 A2、B2、C2 与 ws2(A2、B2、C2)的搜索条件匹配,但是它没有粘贴在 ws3 上?我做错了什么?

4

1 回答 1

0

尝试这个:

Option Explicit
Sub test()

Dim ws1, ws2, ws3 As Worksheet
Dim filter1, filter2, filter3 As String
Dim lrow As Double


Set ws1 = ThisWorkbook.Sheets("Sheet1") 'this contains the text file
Set ws2 = ThisWorkbook.Sheets("Sheet2") 'this contains the filters
Set ws3 = ThisWorkbook.Sheets("Sheet3")' this is the destination

With ws2
    filter1 = "=" & .Range("A2").Value
    filter2 = "=" & .Range("B2").Value
    filter3 = "=" & .Range("C2").Value
End With

With ws1
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row

With .Range("A1:O" & lrow)
        .AutoFilter Field:=5, Criteria1:=filter1
        .AutoFilter Field:=8, Criteria1:=filter2
        .AutoFilter Field:=14, Criteria1:=filter3
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
        ws3.Cells(.Range("A" & .Rows.Count).End(xlUp).Row + 1, 1)
End With

.AutoFilterMode = False

End With

End Sub

我只是假设了列数(A 到 O)
我只是假设了您需要过滤的字段编号。(5, 8, 14)
根据您的需要进行更改。
希望这能让你开始。

于 2013-10-09T10:10:06.030 回答