在自动过滤器列表中选择第一项

时间:2018-12-18 15:58:45

标签: excel vba

使用VBA如何编码以选择自动过滤器列表中的第一项?

目前我有以下内容

ActiveSheet.Range("$A$1:$Y$840").AutoFilter Field:=5, Criteria1:= _
    "October"

但是我想将“十月”更改为下拉列表中的第一项。

致谢

吉姆

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

Option Explicit


Sub calculate()

Dim rn As Range, sh As Worksheet, sh1 As Worksheet


SheetCreate
CopyRange



Set sh = Sheet1 ' the sheet where the data you want to filter are.
Set sh1 = Sheets("help to sort")



With sh

Set rn = sh1.Range("A:A")

If .AutoFilterMode = True Then .AutoFilterMode = False

.Range("$A$1:$Y$840").AutoFilter Field:=5, Criteria1:= _
        rn.Item(2)

End With

End Sub


Sub SheetCreate()

Dim sh As Worksheet, sh1 As Worksheet

For Each sh1 In ActiveWorkbook.Worksheets

If sh1.Name = "help to sort" Then Exit Sub

Next

Set sh = ActiveWorkbook.Sheets.Add

sh.Name = "help to sort"

End Sub


Sub CopyRange()

Dim rn As Range, rn1 As Range,sh As Worksheet, sh1 As Worksheet

Set sh = Sheet1 ' the sheet where the data you want to filter are.
Set sh1 = Sheets("help to sort")

With sh

If sh1.Range("A2") <> "" Then Exit Sub

Set rn = .Range("E:E")
Set rn1 = sh1.Range("A:A")

rn.Copy rn1

rn1.Sort key1:=sh1.Range("A2"), order1:=xlAscending, Header:=xlYes

End With

End Sub

如果愿意,可以将order1更改为xldescending