为VBA筛选选择一些标准

时间:2018-07-27 05:58:57

标签: excel vba filter

这是我在数组建议之后提出的代码。但是,我收到错误消息“对于数组上的每个控制变量必须为Variant”。我究竟做错了什么?

代码:

Sub Filter()

Application.ScreenUpdating = False

Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String
Dim arr(3) As String

'specify sheet name in which the data is stored
sht = "Sheet Name"

'Array
arr(3) = Array("A", "B", "C")

'change filter column in the following code
last = Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:K" & last)

For Each x In arr
With rng
    .AutoFilter
    .AutoFilter Field:=3, Criteria1:=x.Value
    .SpecialCells(xlCellTypeVisible).Copy

Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Paste
End With
Next x

'Turn off filter
Sheets(sht).AutoFilterMode = False

With Application
    .ScreenUpdating = True
    .CutCopyMode = False
End With


End Sub

0 个答案:

没有答案