Скрыть строки, когда данные не соответствуют критериям поиска - PullRequest
0 голосов
/ 29 сентября 2018

У меня есть следующий код для поиска данных на основе ввода пользователя.Я хотел скрыть те строки, которые не совпадают, чтобы пользователи могли лучше видеть.Мои данные начинаются со строки 7. Спасибо.

Sub find_highlight1() 'Unhide and go to the 1st sheet found the data

Dim FindString As String
Dim wrkSht As Worksheet
Dim FoundCell As Range
Dim FirstAddress As String
Dim countfound As Integer
Dim sheetactive As Integer

countfound = 0
sheetactive = 0

FindString = InputBox("Please Key in the Number You Wish to Search")
If FindString = "" Then Exit Sub

'Use For...Each to cycle through the Worksheets collection.
For Each wrkSht In ThisWorkbook.Worksheets
    'Find the first instance on the sheet.
    Set FoundCell = wrkSht.Cells.Find(What:=FindString, After:=wrkSht.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)

     'Check it found something.
     If Not FoundCell Is Nothing Then
         'Save the first address as FIND loops around to the start
         FirstAddress = FoundCell.Address
         wrkSht.Visible = xlSheetVisible 'Unhide worksheet where found
         countfound = countfound + 1
         sheetactive = sheetactive + 1
         If sheetactive <= 1 Then
          wrkSht.Select
     End If

        Do
             With FoundCell.Interior
                 .ColorIndex = 6
                 .Pattern = xlSolid
                 .PatternColorIndex = xlAutomatic

             End With
             'Look for the next instance on the same sheet.
            Set FoundCell = wrkSht.Cells.FindNext(FoundCell)
         Loop While FoundCell.Address <> FirstAddress
     End If

 Next wrkSht

End Sub`

enter image description here

1 Ответ

0 голосов
/ 29 сентября 2018

Попробуйте что-то подобное в своем коде (добавьте другой код в поле ввода для ввода FindString):

    Dim StringValueColumn as long

    StringValueColumn = wrkSht.Cells.Find(What:=FindString, After:=wrkSht.Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False).Column

    MyRange.AutoFilter StringValueColumn, "=[ValueToKeepVisible]"

Обновление

Что-топросто, как это будет работать.

Option Explicit

Public Sub FilterRows()

    Dim MyRange As Range
    Dim FindString As String
    Dim FindRange As Range

    Set MyRange = ActiveSheet.Range("A1").CurrentRegion
    FindString = InputBox("Type a Value")

    Set FindRange = MyRange.Find(What:=FindString)
    If Not FindRange Is Nothing Then
        MyRange.AutoFilter FindRange.Column, FindString
    End If

End Sub

Пример данных

Range("A1:C4")

First       Middle      Last  'Header Values
Brian       Daniel      Smith
Larry       Amos        Smith
Tanya       Esther      Schmidt
...