筛选特定值,而不是包含该值的VBA

时间:2017-01-27 19:16:21

标签: vba

我遇到了问题。我的宏中有一个高级过滤器设置,但是当我想要的时候选择场景2,3,4,当我只想要特定场景时,选择402,403,404,不包含2,3,4这里是我的代码如下。感谢任何帮助.Tks

Sub Filtering()
'
' AdvencedFilterNew Macro
'

    Dim WsOutput As Worksheet
    Dim WsMain As Worksheet
    Dim WsScenarios As Worksheet
    Dim ScenarioIDrow As Long
    Dim ScenarioIDColumn As Long
    Dim rgn As Range
    Dim p As String
    Dim q As String
    Dim f As Range

    Set WsOutput = Worksheets("Output")
    Set WsMain = Worksheets("Main Menu")
    Set WsScenarios = Worksheets("Scenarios")


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = True

    WsOutput.Activate
    Cells.EntireColumn.Hidden = False

    WsMain.Activate
    Range("F15").Select
    Range("F17:H1000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Range("F14:H15"), Unique:=False


    ScenarioIDrow = WsOutput.Cells.Find("Scenario ID").row
    ScenarioIDColumn = WsOutput.Cells(ScenarioIDrow, Columns.Count).End(xlToLeft).Column
    p = WsOutput.Cells(ScenarioIDrow, 2).Address(RowAbsolute:=False, ColumnAbsolute:=False)
    q = WsOutput.Cells(ScenarioIDrow, ScenarioIDColumn).Address(RowAbsolute:=False, ColumnAbsolute:=False)


    WsOutput.Activate
    WsOutput.Range(p, q).Select
    Set f = selection.Find(What:=Worksheets("Main Menu").Range("G15").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    For Each rgn In selection

 If f Is Nothing Then
    rgn.EntireColumn.Hidden = False

    ElseIf rgn = f Then
    rgn.EntireColumn.Hidden = False

    Else
    rgn.EntireColumn.Hidden = True
End If

    Next rgn

    WsMain.Activate
    WsMain.Range("F15").Select

End Sub

所以说Range(" G15")。值是要过滤的数据。

1 个答案:

答案 0 :(得分:1)

试试这个:

Set f = selection.Find(What:=Worksheets("Main Menu").Range("G15").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)