在VBA中使用多个过滤器|| excel 2010

时间:2015-06-04 10:24:36

标签: excel vba excel-vba filter

我是VBA的新手,拼命地需要一些帮助。我有一个excel文件,它有两个工作表,即" URL"和"关键字"。我需要一个可以逐个选择关键字的宏,并将它们作为过滤器应用到" URL"工作表,最后我们有一个包含至少一个关键字的所有网址的列表。

例如,如果我使用第一个关键字" recipe"来应用过滤器,我应该得到,

  • " simplyrecipes.com"
  • " simplerecipewizard.com"

之后,当我应用第二个关键字" net"时,我应该

  • " simplyrecipes.com"
  • " simplerecipewizard.com"
  • " topix.net"
  • " stockinvestingbasics.net"
  • " techdailynews.net"
  • " theanimeplace.net"
  • " seniorhousingnet.com"
  • " wordcounter.net"等等。

您可以在https://goo.gl/wRxNOe

下载Excel文件

日Thnx Satdeep

2 个答案:

答案 0 :(得分:0)

此任务不需要过滤器:

Sub GatherData()
'  http://stackoverflow.com/questions/30641317/using-multiple-filters-in-vba-excel-2010
   Dim s1 As Worksheet, s2 As Worksheet, v1 As String, v2 As String
   Dim N1 As Long, N2 As Long, i As Long, j As Long, K As Long
   Set s1 = Sheets("URLs")
   Set s2 = Sheets("keywords")

   N1 = s1.Cells(Rows.Count, 1).End(xlUp).Row
   N2 = s2.Cells(Rows.Count, 1).End(xlUp).Row
   K = 1

   For j = 1 To N2
      v2 = s2.Cells(j, 1).Value
      For i = 2 To N1
         v1 = s1.Cells(i, 1).Value
         If InStr(v1, v2) > 0 Then
            s1.Cells(i, 1).Copy s2.Cells(K, 2)
            K = K + 1
         End If
      Next i
   Next j
End Sub

答案 1 :(得分:0)

能够找到一个解决方法。添加了两个宏,并在Gary的学生上面提供的第一个宏中调用它们。

以下是使用的两个宏:

<ul class="navlist">
  <li><a href="What'son.html">What's on</a></li>
  <li><a href="History.html">History</a></li>
  <li><a href="specialoffers.html">Special offers</a></li>
  <li><a href="contactus.html">contat us</a></li>
  <li><a href="otherstores.html">other stores</a></li>
</ul>
Worksheets("URLs").Activate
Range("B2").Activate
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Result!C1,1,0)"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B2716")
Range("B2:B2716").Select
Range("B1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$2716").AutoFilter Field:=2, Criteria1:= _
    "=#VALUE!", Operator:=xlOr, Criteria2:="=#N/A"
Range("A1:B2716").Select
Range("B1").Activate
Selection.Copy
Sheets("Approved List").Select
Range("A1").Select
ActiveSheet.Paste
Columns("B:B").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft

感谢大家帮助我这里