User Form ListBox - Find Records

LONeill13

Board Regular
Joined
Feb 12, 2013
Messages
135
Greetings Again:

Have a quick questions, need some help with the List Box on my User form - which will display all records found that match the member # that is entered. The only issue I have with the list box is that when it is populated with the records found, it does not display the data properly - the header should appear in the header row, but it appears that when the list box is populated, the data is being shifted down two rows. I'm sure this a simple fix, but I'm stumped with this as well.

Here is my code for find, find all, and populating the List Box. Any help with this would be appreciated - it's driving me nuts!

Code:
[TABLE="width: 391"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Private Sub cmdFind_Click()[/TD]
[/TR]
[TR]
[TD]    'Set Variables[/TD]
[/TR]
[TR]
[TD]    Dim strFind As String    'what to find[/TD]
[/TR]
[TR]
[TD]    Dim FirstAddress As String[/TD]
[/TR]
[TR]
[TD]    Dim rSearch As Range  'range to search[/TD]
[/TR]
[TR]
[TD]    Set rSearch = Sheets("FraudTracker").Range("a2", Range("a65536").End(xlUp))     'Search from the last row up till cell A2 is reached[/TD]
[/TR]
[TR]
[TD]    Dim f As Integer            'Number or records returned in search[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]    imgFolder = ThisWorkbook.Path & Application.PathSeparator & "images" & Application.PathSeparator[/TD]
[/TR]
[TR]
[TD]    [/TD]
[/TR]
[TR]
[TD]    'Search for the data in the MemberNumber Text Box[/TD]
[/TR]
[TR]
[TD]    strFind = Me.tbMemNum.Value[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]    With rSearch[/TD]
[/TR]
[TR]
[TD]        'Search all rows for strFind[/TD]
[/TR]
[TR]
[TD]        Set c = .Find(strFind, LookIn:=xlValues)[/TD]
[/TR]
[TR]
[TD]        [/TD]
[/TR]
[TR]
[TD]        'If data is found load the rest of that row into the form[/TD]
[/TR]
[TR]
[TD]        If Not c Is Nothing Then[/TD]
[/TR]
[TR]
[TD]            c.Select[/TD]
[/TR]
[TR]
[TD]            [/TD]
[/TR]
[TR]
[TD]            'Loads Form[/TD]
[/TR]
[TR]
[TD]            'TextBox.Value is the Text Box to be populated[/TD]
[/TR]
[TR]
[TD]            'c.Offset(0, X).Value means from column A, offset X number if cells[/TD]
[/TR]
[TR]
[TD]            'Column A is (0, 0).  Column B is (0, 1).  Column F is (0, 5).  Etc.[/TD]
[/TR]
[TR]
[TD]            With Me[/TD]
[/TR]
[TR]
[TD]                .tbMemNum.Value = c.Value[/TD]
[/TR]
[TR]
[TD]                    .tbDate1.Value = c.Offset(0, 1).Value[/TD]
[/TR]
[TR]
[TD]                    .cboIssueType.Value = c.Offset(0, 2).Value[/TD]
[/TR]
[TR]
[TD]                    .cboIssueReportedBy.Value = c.Offset(0, 3).Value[/TD]
[/TR]
[TR]
[TD]                    .tbDateIssue.Value = c.Offset(0, 4).Value[/TD]
[/TR]
[TR]
[TD]                    .cboIssueStatus.Value = c.Offset(0, 5).Value[/TD]
[/TR]
[TR]
[TD]                    .tbSource.Value = c.Offset(0, 6).Value[/TD]
[/TR]
[TR]
[TD]                    .tbOpenDate.Value = c.Offset(0, 7).Value[/TD]
[/TR]
[TR]
[TD]                    .tbEnrollMeth.Value = c.Offset(0, 8).Value[/TD]
[/TR]
[TR]
[TD]                    .cboUI.Value = c.Offset(0, 9).Value[/TD]
[/TR]
[TR]
[TD]                    .FName.Caption = c.Offset(0, 10).Value[/TD]
[/TR]
[TR]
[TD]                    .LName.Caption = c.Offset(0, 11).Value[/TD]
[/TR]
[TR]
[TD]                    .Address.Caption = c.Offset(0, 12).Value[/TD]
[/TR]
[TR]
[TD]                    .City.Caption = c.Offset(0, 13).Value[/TD]
[/TR]
[TR]
[TD]                    .State.Caption = c.Offset(0, 14).Value[/TD]
[/TR]
[TR]
[TD]                    .Zip.Caption = c.Offset(0, 15).Value[/TD]
[/TR]
[TR]
[TD]                    .tbFraud.Value = c.Offset(0, 16).Value[/TD]
[/TR]
[TR]
[TD]                    .tbBonusPt.Value = c.Offset(0, 17).Value[/TD]
[/TR]
[TR]
[TD]                    .tbGoldPt.Value = c.Offset(0, 18).Value[/TD]
[/TR]
[TR]
[TD]                    .tbOtherPt.Value = c.Offset(0, 19).Value[/TD]
[/TR]
[TR]
[TD]                    .tbPtsRedeemed.Value = c.Offset(0, 20).Value[/TD]
[/TR]
[TR]
[TD]                    .tbPoint.Value = c.Offset(0, 21).Value[/TD]
[/TR]
[TR]
[TD]                    .tbDollar.Value = c.Offset(0, 22).Value[/TD]
[/TR]
[TR]
[TD]                    .cboSiteID.Value = c.Offset(0, 23).Value[/TD]
[/TR]
[TR]
[TD]                    .tbSummary.Value = c.Offset(0, 28).Value[/TD]
[/TR]
[TR]
[TD]                    .cboCredit.Value = c.Offset(0, 29).Value[/TD]
[/TR]
[TR]
[TD]                    .tbCrAmt.Value = c.Offset(0, 30).Value[/TD]
[/TR]
[TR]
[TD]                    .tbCrDate.Value = c.Offset(0, 31).Value[/TD]
[/TR]
[TR]
[TD]                .cmdEdit.Enabled = True        'allow for record to be amended[/TD]
[/TR]
[TR]
[TD]                .cmdClose.Enabled = True           'allow record deletion[/TD]
[/TR]
[TR]
[TD]                .cmdAdd.Enabled = True              'allow for new record to be created[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]                f = 0[/TD]
[/TR]
[TR]
[TD]            End With[/TD]
[/TR]
[TR]
[TD]        [/TD]
[/TR]
[TR]
[TD]            FirstAddress = c.Address[/TD]
[/TR]
[TR]
[TD]            Do[/TD]
[/TR]
[TR]
[TD]                f = f + 1    'count number of matching records[/TD]
[/TR]
[TR]
[TD]                Set c = .FindNext(c)[/TD]
[/TR]
[TR]
[TD]            Loop While Not c Is Nothing And c.Address <> FirstAddress[/TD]
[/TR]
[TR]
[TD]            [/TD]
[/TR]
[TR]
[TD]            'If multiple entries are found, return a message box to aleart the user[/TD]
[/TR]
[TR]
[TD]            If f > 1 Then[/TD]
[/TR]
[TR]
[TD]                Select Case MsgBox("There are " & f & " instances of " & strFind, vbOKCancel Or vbExclamation Or vbDefaultButton1, "Multiple entries")[/TD]
[/TR]
[TR]
[TD]                    [/TD]
[/TR]
[TR]
[TD]                    'If user clicks OK, exceute the FindAll function[/TD]
[/TR]
[TR]
[TD]                    Case vbOK[/TD]
[/TR]
[TR]
[TD]                        FindAll[/TD]
[/TR]
[TR]
[TD]                    'If user clicks Cancel, exit out of this funciton[/TD]
[/TR]
[TR]
[TD]                    Case vbCancel[/TD]
[/TR]
[TR]
[TD]         [/TD]
[/TR]
[TR]
[TD]                End Select[/TD]
[/TR]
[TR]
[TD]                Me.Height = 750[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]            End If[/TD]
[/TR]
[TR]
[TD]            [/TD]
[/TR]
[TR]
[TD]        'If no matching data is found, pop up a message box to inform the user[/TD]
[/TR]
[TR]
[TD]        Else: MsgBox strFind & " not listed"    'search failed[/TD]
[/TR]
[TR]
[TD]        End If[/TD]
[/TR]
[TR]
[TD]    End With[/TD]
[/TR]
[TR]
[TD]    If Sheets("FraudTracker").AutoFilterMode Then Sheets("FraudTracker").Range("A2").AutoFilter[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]'**************************************************************************************[/TD]
[/TR]
[TR]
[TD]'FindAll Function[/TD]
[/TR]
[TR]
[TD]'Finds all records matching the search from Search by Name and returns them to a List Box[/TD]
[/TR]
[TR]
[TD]'**************************************************************************************[/TD]
[/TR]
[TR]
[TD]    [/TD]
[/TR]
[TR]
[TD]Sub FindAll()[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]'Set Variables[/TD]
[/TR]
[TR]
[TD]Dim strFind As String 'what to find[/TD]
[/TR]
[TR]
[TD]Dim rFilter As Range 'range to search[/TD]
[/TR]
[TR]
[TD]Dim c As Range, a() As String, n As Long, I As Long[/TD]
[/TR]
[TR]
[TD]Set rFilter = Sheets("FraudTracker").Range("A2", Range("a65536").End(xlUp))[/TD]
[/TR]
[TR]
[TD]Set rng = Sheets("FraudTracker").Range("A2", Range("a65536").End(xlUp))[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]strFind = Me.tbMemNum.Value     'Search value is MemberNumber[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]With Sheet1[/TD]
[/TR]
[TR]
[TD]    If Not .AutoFilterMode Then .Range("A2").AutoFilter[/TD]
[/TR]
[TR]
[TD]    rFilter.AutoFilter Field:=1, Criteria1:="*" & strFind & "*"[/TD]
[/TR]
[TR]
[TD]    Set rng = rng.Cells.SpecialCells(xlCellTypeVisible)[/TD]
[/TR]
[TR]
[TD]    [/TD]
[/TR]
[TR]
[TD]    'Clear any data currently in the List Box[/TD]
[/TR]
[TR]
[TD]    Me.ListBox1.Clear[/TD]
[/TR]
[TR]
[TD]    [/TD]
[/TR]
[TR]
[TD]    'For each found entry return columns 0 to 32[/TD]
[/TR]
[TR]
[TD]    For Each c In rng[/TD]
[/TR]
[TR]
[TD]        n = n + 1: ReDim Preserve a(0 To 32, 0 To n)[/TD]
[/TR]
[TR]
[TD]        For I = 0 To 32[/TD]
[/TR]
[TR]
[TD]            a(I, n) = c.Offset(, I).Value[/TD]
[/TR]
[TR]
[TD]        Next[/TD]
[/TR]
[TR]
[TD]    Next[/TD]
[/TR]
[TR]
[TD]End With[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]'For each record found, enter it into the List Box[/TD]
[/TR]
[TR]
[TD]If n > 0 Then Me.ListBox1.Column = a[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]'ListBox Function[/TD]
[/TR]
[TR]
[TD]'Takes the data found between the search function and the FindAll function and inserts[/TD]
[/TR]
[TR]
[TD]'the basic data into a List Box where a user can then select the proper record to edit or delete[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Private Sub ListBox1_Click()[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]    'Checks that there is data to be entered into the listbox.[/TD]
[/TR]
[TR]
[TD]    'If there isn't it pops up a message box[/TD]
[/TR]
[TR]
[TD]    If Me.ListBox1.ListIndex = -1 Then    'not selected[/TD]
[/TR]
[TR]
[TD]        MsgBox " No selection made"[/TD]
[/TR]
[TR]
[TD]        [/TD]
[/TR]
[TR]
[TD]    'If data is found, the populate the List Box[/TD]
[/TR]
[TR]
[TD]    ElseIf Me.ListBox1.ListIndex >= 1 Then    'User has selected[/TD]
[/TR]
[TR]
[TD]        r = Me.ListBox1.ListIndex[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]        'TextBox.Value is the Text Box where the data is coming from[/TD]
[/TR]
[TR]
[TD]        'ListBox1.List(r, X) is the cell in the List Box data is entered into[/TD]
[/TR]
[TR]
[TD]        'Column A is (r, 0).  Column B is (r, 1).  Column F is (r, 5).  Etc.[/TD]
[/TR]
[TR]
[TD]        'r equals the row of the List Box data is being entered into.[/TD]
[/TR]
[TR]
[TD]        With Me[/TD]
[/TR]
[TR]
[TD].tbDate1.Value = ListBox1.List(r, 1)[/TD]
[/TR]
[TR]
[TD].cboIssueType.Value = ListBox1.List(r, 2)[/TD]
[/TR]
[TR]
[TD].cboIssueReportedBy.Value = ListBox1.List(r, 3)[/TD]
[/TR]
[TR]
[TD].tbDateIssue.Value = ListBox1.List(r, 4)[/TD]
[/TR]
[TR]
[TD].cboIssueStatus.Value = ListBox1.List(r, 5)[/TD]
[/TR]
[TR]
[TD].tbSource.Value = ListBox1.List(r, 6)[/TD]
[/TR]
[TR]
[TD].tbMemNum.Value = ListBox1.List(r, 0)[/TD]
[/TR]
[TR]
[TD].tbOpenDate.Value = ListBox1.List(r, 7)[/TD]
[/TR]
[TR]
[TD].tbEnrollMeth.Value = ListBox1.List(r, 8)[/TD]
[/TR]
[TR]
[TD].cboUI.Value = ListBox1.List(r, 9)[/TD]
[/TR]
[TR]
[TD].FName.Caption = ListBox1.List(r, 10)[/TD]
[/TR]
[TR]
[TD].LName.Caption = ListBox1.List(r, 11)[/TD]
[/TR]
[TR]
[TD].Address.Caption = ListBox1.List(r, 12)[/TD]
[/TR]
[TR]
[TD].City.Caption = ListBox1.List(r, 13)[/TD]
[/TR]
[TR]
[TD].State.Caption = ListBox1.List(r, 14)[/TD]
[/TR]
[TR]
[TD].Zip.Caption = ListBox1.List(r, 15)[/TD]
[/TR]
[TR]
[TD].tbFraud.Value = ListBox1.List(r, 16)[/TD]
[/TR]
[TR]
[TD].tbBonusPt.Value = ListBox1.List(r, 17)[/TD]
[/TR]
[TR]
[TD].tbGoldPt.Value = ListBox1.List(r, 18)[/TD]
[/TR]
[TR]
[TD].tbOtherPt.Value = ListBox1.List(r, 19)[/TD]
[/TR]
[TR]
[TD].tbPtsRedeemed.Value = ListBox1.List(r, 20)[/TD]
[/TR]
[TR]
[TD].tbPoint.Value = ListBox1.List(r, 21)[/TD]
[/TR]
[TR]
[TD].tbDollar.Value = ListBox1.List(r, 22)[/TD]
[/TR]
[TR]
[TD].cboSiteID.Value = ListBox1.List(r, 23)[/TD]
[/TR]
[TR]
[TD].tbSummary.Value = ListBox1.List(r, 28)[/TD]
[/TR]
[TR]
[TD].cboCredit.Value = ListBox1.List(r, 29)[/TD]
[/TR]
[TR]
[TD].tbCrAmt.Value = ListBox1.List(r, 30)[/TD]
[/TR]
[TR]
[TD].tbCrDate.Value = ListBox1.List(r, 31)[/TD]
[/TR]
[TR]
[TD]            .cmdEdit.Enabled = True        'Allow for Amendment by Name[/TD]
[/TR]
[TR]
[TD]            .cmdClose.Enabled = True           'Allow for record Deletion[/TD]
[/TR]
[TR]
[TD]            .cmdAdd.Enabled = True              'Allow to add a new record[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]        End With[/TD]
[/TR]
[TR]
[TD]        [/TD]
[/TR]
[TR]
[TD]        'move to the next row of the List Box[/TD]
[/TR]
[TR]
[TD]        r = r - 1[/TD]
[/TR]
[TR]
[TD]    End If[/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top