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
歡迎StackOverflow上,比爾你有一個屏幕截圖或兩個支持上述說明這裏是一個偉大的網站[幫你改善你的帖子](http://stackoverflow.com/help/how-to-ask)。可能有一些指標可以幫助你。也許你也可以縮短上面的帖子(或者我所有這些都是相關/必要的)。請不要誤解。但是,你在你的帖子中是正確的,並且跟隨(至少對我而言)有點難。但是,也許你不需要改變任何東西,這裏的其他人可以關注並幫助你解決問題。 – Ralph
嗨拉爾夫 - 謝謝你的回覆。我編輯了我原來的帖子,希望能夠讓問題更清楚。 – Bill