Results 1 to 3 of 3

Thread: Find keyword using multiple Text box and combox value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try this...

    Code:
    Private Sub CommandButton1_Click()
        Dim varCriteria As Variant
        Dim varIndex As Variant
        Dim lngElements As Long
        
        lngElements = Abs(Me.txtNo.Value <> "") + Abs(Me.txtName.Value <> "") + Abs(Me.txtParts.Value <> "")
        ReDim varCriteria(1 To lngElements)
        ReDim varIndex(1 To lngElements)
        lngElements = 0
        If Me.txtNo.Value <> "" Then
            lngElements = lngElements + 1
            varCriteria(lngElements) = Me.txtNo.Value
            varIndex(lngElements) = 1
        End If
        
        If Me.txtName.Value <> "" Then
            lngElements = lngElements + 1
            varCriteria(lngElements) = Me.txtName.Value
            varIndex(lngElements) = 2
        
        End If
        If Me.txtParts.Value <> "" Then
            lngElements = lngElements + 1
            varCriteria(lngElements) = Me.txtParts.Value
            varIndex(lngElements) = 3
        End If
        Consolidator varCriteria, varIndex, Worksheets(Me.cmbSearchName.Value)
    End Sub
    Function Consolidator(varCriteria As Variant, varIndex As Variant, wks As Worksheet)
        Dim varSource As Variant
        Dim lngElements As Long
        Dim lngRows As Long
        Dim blnValid As Boolean
        Dim varOutput As Variant
        Dim lngCounter As Long
        
        With wks
            varSource = .Range("B4:G" & .Cells(Rows.Count, 2).End(xlUp).Row)
        End With
        ReDim varOutput(1 To UBound(varSource), 1 To UBound(varSource, 2))
        For lngRows = LBound(varSource) To UBound(varSource)
            For lngElements = LBound(varCriteria) To UBound(varCriteria)
                If UCase(varSource(lngRows, varIndex(lngElements))) = UCase(varCriteria(lngElements)) Then
                    blnValid = True
                Else
                    blnValid = False
                    Exit For
                End If
            Next lngElements
            If blnValid Then
                lngCounter = lngCounter + 1
                For lngElements = 1 To UBound(varSource, 2)
                    varOutput(lngCounter, lngElements) = varSource(lngRows, lngElements)
                Next lngElements
            End If
        Next lngRows
        With Worksheets("Main")
            .Range("B21:G" & .Cells(Rows.Count, 2).End(xlUp).Row + 2).ClearContents
            .Range("B21").Resize(UBound(varOutput), UBound(varOutput, 2)).Value = varOutput
        End With
        
    End Function
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Member Ryan_Bernal's Avatar
    Join Date
    Dec 2012
    Posts
    37
    Rep Power
    0
    Thank you so much admin.
    It works like a magic!
    Last edited by Ryan_Bernal; 03-07-2013 at 06:50 PM.

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2013, 11:31 AM
  2. Linking a table to a text box
    By Safal Shrestha in forum Excel Help
    Replies: 6
    Last Post: 04-25-2013, 10:37 AM
  3. Find a text substring that matches a given "pattern"
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 2
    Last Post: 02-10-2013, 06:19 AM
  4. Replies: 2
    Last Post: 09-25-2012, 01:30 AM
  5. Find the First or Last So Many Words in a Text String
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 6
    Last Post: 06-21-2012, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •