简体   繁体   中英

Checkbox search in excel VBA

this is my first time in this forum. Im a 'newbie' in excel VBA and would appreciate if anyone can help.

I am trying to develop a checkbox search page, where:

  • the user is able to use checkbox select up to 4 items to search (in 'Search' worksheet)
  • In the data sheet, the data is recorded (by columns) as follows:

Code Book Item Material 1 Percentage 1 Material 2 Percentage 2 Material 3 Percentage 3 Material 4 Percentage 4

  • an item, eg "Polyester", will appear in columns 'material 1', 2 and so on, depending on the record
  • the search should be based on the checkboxes that are checked in the search page
  • if multiple keywords are checked, eg polyester, wool, cotton, then only rows that has all 3 keywords should be shown

I have searched numerous posts online, and there's one where they delete rows if the keywords are found. Is there any way i can twist the logic and apply to this particular situation?

Been stuck for many days... grateful if anyone can help!

Update 1: I have managed to find something on the internet that deletes the rows if certain keywords are found - however - i would like to do the opposite (this would leave me the remaining correct ones instead...)

Code as below - is it possible to change?:

Sub Example1()

    Dim varList As Variant
    Dim lngarrCounter As Long
    Dim rngFound As Range, rngToDelete As Range
    Dim strFirstAddress As String

    Application.ScreenUpdating = False

    varList = VBA.Array("Here", "There", "Everywhere")

    For lngarrCounter = LBound(varList) To UBound(varList)
        With Sheet1.UsedRange
            Set rngFound = .Find( _
                                What:=varList(lngarrCounter), _
                                Lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=True)

            If Not rngFound Is Nothing Then
                strFirstAddress = rngFound.Address

                If rngToDelete Is Nothing Then
                    Set rngToDelete = rngFound
                Else
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                End If

                Set rngFound = .FindNext(After:=rngFound)

                Do Until rngFound.Address = strFirstAddress
                    If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
                        Set rngToDelete = Application.Union(rngToDelete, rngFound)
                    End If
                    Set rngFound = .FindNext(After:=rngFound)
                Loop
            End If
        End With
    Next lngarrCounter

    If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete

    Application.ScreenUpdating = True

Perhaps you could try the Sheets("Search").Cells.Find([keyword]) command which is similiar to the ctrl+L keys.

If you wish to search more than 1 keyword, you could use the following loop:

dim i as Integer
dim vTemp as Variant
dim strFind as String
dim objSearch as Object

strFind = range("searchField").value
vTemp = split(strFind, ",")

for i =0 to Ubound(vTemp)
    Set objSearch  = Sheets("Search").Cells.Find(vTemp(i))
    'Do whatever you like to the object of search
next i 

I hope I could help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM