这应该是有效的:
Dim ws As Worksheet
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = "TEMP"
With s1
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(Application.Subtotal(103, .Cells)) Then
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
.Copy
ws.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
End With
End With
或者一些完全修改的代码:
Sub copypaste()
Dim i, j, v As Long
Dim vSearchCols As Variant
Dim vCols As Variant
Dim FilterFor As String
Dim ws As Worksheet
Dim s1 As Worksheet
Dim s2 As Worksheet
FilterFor = "=AF*"
With ThisWorkbook
Set s1 = .Worksheets("RAW DATA")
Set s2 = .Worksheets("AF SITE TYPE")
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
ws.Name = "TEMP"
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
vSearchCols = Array("Prefix+short name", "Site type", "Probe Id", "Owner", "SLA Target", "Avg RTT (ms)", "Completion (ms)")
ReDim vCols(0 To UBound(vSearchCols))
For v = LBound(vSearchCols) To UBound(vSearchCols)
vCols(v) = s1.Rows(2).Cells.Find(What:=vSearchCols(v), LookIn:=xlFormulas, LookAt:=xlWhole).Column
Next v
With s1
.AutoFilterMode = False
With .Range("A1").CurrentRegion
.AutoFilter Field:=vCols(0), Criteria1:=FilterFor
If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
.Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
ws.Range("A1").PasteSpecial Paste:=xlPasteValues
End If
End With
End With
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub