可以循環使用計數器:
Sub FilterCDA()
Dim sh1 As Worksheet
Dim N As Long
Dim TopVisibleCell As Range
Dim sh2 As Worksheet
Dim HeaderRow As Long
Dim LastFilterRow As Long
Dim st As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim VTR As String
Dim W As Integer
Dim R As Integer
Dim NOC As Range
Dim NOC1 As Integer
Dim rSelect As Range
Dim rCell As Range
Set sh1 = Sheets("Request")
Set sh2 = Sheets("Request")
C = 2
Set NOC = sh2.Range("D2")
NOC1 = NOC.Value
LR = Worksheets("ORT").Range("A" & Rows.Count).End(xlUp).Row
Set Rstatus1 = Worksheets("ORT").Range("G2:G" & LR)
Set Rstatus = Worksheets("ORT").Range("A1:G" & LR)
N = sh1.Cells(Rows.Count, "C").End(xlUp).Row
Sheets("CSV").Cells.NumberFormat = "@"
For i = 2 To N
v = sh1.Cells(i, 3).Value
If v <> "" Then
st = st & v & ","
End If
Next i
st = Mid(st, 1, Len(st) - 1)
Arr1 = Split(st, ",")
Sheets("ORT").Activate
For i = LBound(Arr1) To UBound(Arr1)
Sheets("ORT").AutoFilterMode = False
With Sheets("ORT").Range("A:G")
.AutoFilter Field:=3, Criteria1:=Arr1(i), Operator:=xlFilterValues
End With
Fr = Worksheets("ORT").Range("C" & Rows.Count).End(xlUp).Row - 1
' No rows filtered then Fr = 0
If Fr > 0 Then
With Rstatus
Set rVis = .Resize(.Rows.Count - 1, 1).Offset(1).SpecialCells(xlCellTypeVisible)
End With
For Each rCell In rVis.Cells
If rSelect Is Nothing Then
Set rSelect = rCell.Resize(, Rstatus.Columns.Count)
Else
Set rSelect = Union(rSelect, rCell.Resize(, Rstatus.Columns.Count))
End If
lCounter = lCounter + 1
If lCounter >= NOC1 Then Exit For
Next rCell
rSelect.Copy
Sheets("CSV").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues
ElseIf Fr = 0 Then
End If
Set NOC = NOC.Offset(1)
NOC1 = NOC.Value
Next i
Sheets("ORT").AutoFilterMode = False
Sheets("Request").Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF('CSV'!C[-2],'Request'!RC[-2])"
On Error Resume Next
Selection.AutoFill Destination:=Range("E2:E" & Range("C" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy
Columns("E:E").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Control").Select
Range("A1").Select
End Sub
請參閱編輯並鏈接到示例工作簿。我很抱歉在我的初始文章中沒有完全清楚。謝謝 – user2298601
@加里的學生請參閱編輯和鏈接到示例工作簿。我很抱歉在我的初始文章中沒有完全清楚。謝謝 – user2298601
@ user2298601代碼已更改爲與您的工作簿相匹配:-) – JosieP