2016-09-29 39 views
1

修订后的问题:以下代码将过滤器添加到arrResults。当宏通过arrResults()过滤字段13时,它仅按捕获的最后一个值进行过滤。当运行调试和查看立即窗口时,我确实看到arrResults()中捕获了多个值,但代码仅在最后一个条目中进行过滤(即,如果单击“Received/Sent/Fail”,则只返回“Fail”)。通过IF和多个标准自动过滤器

Option Explicit 

Sub Add_Sheet_Update() 
Dim LastRow As Long 
Dim Rng As Range, str1 As String, str2 As String 
Dim i As Long, wsName As String, temp As String 
Dim arrResults() 

With Sheets("All Call Center Detail") 
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row 
    Set Rng = .Range("A1:BT" & LastRow) 
End With 

With Sheets("Search Form") 
    str1 = .Range("E9").Text 
    str2 = .Range("E13").Text 
End With 

Dim x As Integer, y As Integer 
With Range("R1:S99") ' 2 columns, any # of rows 
    For x = 1 To .Rows.Count 
     If .Cells(x, 1) Then 
      y = y + 1 
      ReDim Preserve arrResults(1 To y) 
      arrResults(y) = .Cells(x, 2) 
      End If 
    Next x 
End With 
Debug.Print Join(arrResults, "/") 

Sheets.Add After:=Sheets("Search Form") 
ActiveSheet.Name = ("Results") 

Sheets("All Call Center Detail").Select 
If Not str1 = "" Then Rng.AutoFilter Field:=6, Criteria1:=str1 
If Not str2 = "" Then Rng.AutoFilter Field:=7, Criteria1:=str2 
If y > 0 Then Rng.AutoFilter Field:=13, Criteria1:=arrResults 

Rng.SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Results").Range ("A1") 

Application.CutCopyMode = False 
ActiveSheet.ShowAllData 

Sheets("Results").Activate 
ActiveSheet.Columns.AutoFit 
wsName = Format(Date, "mmddyy") 
If WorksheetExists(wsName) Then 
    temp = Left(wsName, 6) 
    i = 1 
    wsName = temp & "_" & i 
    Do While WorksheetExists(wsName) 
     i = i + 1 
     wsName = temp & "_" & i 
    Loop 
End If 

ActiveSheet.Name = wsName 
Range("A1").Select 

End Sub 
+1

欢迎StackOverflow上,比尔你有一个屏幕截图或两个支持上述说明这里是一个伟大的网站[帮你改善你的帖子](http://stackoverflow.com/help/how-to-ask)。可能有一些指标可以帮助你。也许你也可以缩短上面的帖子(或者我所有这些都是相关/必要的)。请不要误解。但是,你在你的帖子中是正确的,并且跟随(至少对我而言)有点难。但是,也许你不需要改变任何东西,这里的其他人可以关注并帮助你解决问题。 – Ralph

+1

嗨拉尔夫 - 谢谢你的回复。我编辑了我原来的帖子,希望能够让问题更清楚。 – Bill

回答

0

Operator参数添加到您的AutoFilter声明:?

If y > 0 Then Rng.AutoFilter Field:=13, Criteria1:=arrResults, Operator:=xlFilterValues