+ Reply to Thread
Results 1 to 2 of 2

User Forms for search

  1. #1
    Registered User
    Join Date
    10-11-2011
    Location
    worcester, ma
    MS-Off Ver
    Excel 2003
    Posts
    1

    User Forms for search

    was wondering if anyone can please help me with this task I would to create a userform that will search a workbook in excel with mulitiple sheet, it could be 5-endless worksheets The way i would like to do this is a new tab is created when a new client is add to our database and be able to seach curtain critiera such Hospital name, Contact name,services,phone numbers(if possible)and User name (if Possible) It sounds more complacted then it seems i am going to try to attach the file and code is there anyone whom can help i will be thankful


    [Option Explicit]

    'Module Level Variables
    Dim rRange As Range
    Dim strFind1 As String
    Dim strFind2 As String
    Dim strFind3 As String


    Private Sub ComboBox1_Change()
    'Pass chosen value to String variable strFind1
    strFind1 = ComboBox1
    'Enable ComboBox2 only if value is chosen
    ComboBox2.Enabled = Not strFind1 = vbNullString
    End Sub
    Private Sub ComboBox2_Change()
    'Pass chosen value to String variable strFind1
    strFind2 = ComboBox2
    'Enable ComboBox3 only if value is chosen
    End Sub


    Private Sub CommandButton1_Click()
    'Procedure level variables
    Dim lCount As Long
    Dim lOccur As Long
    Dim rCell As Range
    Dim rCell2 As Range
    Dim rCell3 As Range
    Dim bFound As Boolean

    'At least one value, from ComboBox1 must be chosen
    If strFind1 & strFind2 & strFind3 = vbNullString Then
    MsgBox "No items to find chosen", vbCritical
    Exit Sub 'Go no further
    ElseIf strFind1 = vbNullString Then
    MsgBox "A value from " & Label1.Caption _
    & " must be chosen", vbCritical
    Exit Sub 'Go no further
    End If

    'Clear any old entries
    On Error Resume Next
    ListBox1.Clear
    On Error GoTo 0

    'If String variable are empty pass the wildcard character
    If strFind2 = vbNullString Then strFind2 = "*"
    If strFind3 = vbNullString Then strFind3 = "*"

    'Set range variable to first cell in table.
    Set rCell = rRange.Cells(1, 1)
    'Pass the number of times strFind1 occurs
    lOccur = WorksheetFunction.CountIf(rRange.Columns(1), strFind1)

    'Loop only as many times as strFind1 occurs
    For lCount = 1 To lOccur
    'Set the range variable to the found cell. This is then also _
    used to start the next Find from (After:=rCell)
    Set rCell = rRange.Columns(1).Find(What:=strFind1, After:=rCell, _
    LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False)
    'Check each find to see if strFind2 and strFind3 occur _
    on the same row.
    If rCell(1, 2) Like strFind2 And rCell(1, 3) Like strFind3 Then
    bFound = True 'Used to not show message box for no value found.
    'Add the address of the found cell and the cell on the _
    same row but 2 columns to the right.
    ListBox1.AddItem rCell.Address & ":" & rCell(1, 3).Address
    End If
    Next lCount

    If bFound = False Then 'No match
    MsgBox "Sorry, no matches", vbOKOnly
    End If
    End Sub

    Private Sub CommandButton2_Click()
    'Close UserForm
    Unload Me
    End Sub


    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    'Check for range addresses
    If ListBox1.ListCount = 0 Then Exit Sub
    'GoTo doubled clicked address
    Application.Goto Range(ListBox1.Text), True
    End Sub

    Private Sub UserForm_Initialize()
    'Procedure level module
    Dim lRows As Long

    'Set Module level range variable to CurrentRegion _
    of the Selection
    Set rRange = Selection.CurrentRegion
    If rRange.Rows.Count < 2 Then ' Only 1 row
    MsgBox "Please select any cell in your table first", vbCritical
    Unload Me 'Close Userform
    Exit Sub
    Else

    With rRange
    'Set Label Captions to the Table headings
    Label1.Caption = .Cells(1, 1)
    Label2.Caption = .Cells(1, 2)
    Label3.Caption = .Cells(1, 3)

    'Set RowSource of ComboBoxes to the appropriate columns _
    inside the table
    strSheet = ListBox1.List(ListBox1.ListIndex, 1)
    strAddress = ListBox1.List(ListBox1.ListIndex, 2)


    End With
    End If
    End Sub

    Private Sub UserForm_Terminate()
    'Destroy Module level variables
    Set rRange = Nothing
    strFind1 = vbNullString
    strFind2 = vbNullString
    End Sub
    Attached Files
    Attached Files Attached Files

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,695

    Re: User Forms for search proposes

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Audere est facere

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1