使用多个值从另一范围提取多个匹配项

时间:2019-01-14 08:51:51

标签: excel vba loops

我有一个数据列表(一个带有值的列),我想将其设置为“值”,以便从另一个范围提取数据。我知道Value只能处理一个单元格,是否有解决方法?

enter image description here

  • 第1步,在G列中设置一个值范围
  • 第2步,在A列中找到匹配的结果
  • 第3步,将其粘贴到G列到L中(有时,每个匹配的reslut会有多于一行的数据,因此,第1步中的值X在第2步中可能会累加一次以上。)

Sub finddata() 
    Dim place As Long
    Dim FinalRow As Long
    Dim i As Integer

    Set place = Sheets(1).Range("h2:H10").Value

    FinalRow = Sheets(1).Range("A10000").End(xlUp).Row

    For i = 2 To FinalRow
        If Sheets(1).Cells(i, 1) = place Then
            Sheets(1).Range(Cells(i, 1), Cells(i, 5)).Copy
            Sheets(1).Range("H100").End(xlUp).Offset(1, 0).PasteSpecial       xlPasteFormulasAndNumberFormats
       End If
    Next i
End Sub

好的,这就是我现在使用自动过滤器所达到的程度。现在的问题是,它仅从获取列表中过滤了第一行。换句话说,来自“ G2”的值

 Sub hamta()




 Dim ary As Variant

 With Worksheets(1).Cells(1, 1).CurrentRegion
 'with true numbers in column A this DOES NOT work
   ary = Range("g2:g5").Value
   AutoFilter field:=1, Criteria1:=ary, Operator:=xlFilterValues
  'with true numbers in column A this DOES work

 End With

 End Sub

0 个答案:

没有答案