UserForm Next Record Same Last Name

LikeButtah1

New Member
Joined
Apr 17, 2018
Messages
34
Hello All,
I am trying to find the next record of the same last name that the user will type into textbox1. I can have another button to navigate through the records but after finding the last record it should say "Last Record Found". Column A holds the last names and first record starts in A3. Here is the code that finds the first instance of the last name in textbox1:

Code:
Dim blnNew As Boolean
Dim TRows, i As Long




Private Sub btnLName_Click()


                blnNew = False
                txtLastName.Text = ""
                txtFirstName.Text = ""
                txtC.Text = ""
                txtNewCard.Text = ""
                txtAccDes.Text = ""
                txtAccCreated.Text = ""
                txtAccGranted.Text = ""
                txtAccCancelled.Text = ""
                txtAccCategory.Text = ""
                txtNotes.Text = ""
   
    TRows = Worksheets("PNG Database").Range("A1").CurrentRegion.Rows.Count
    For i = 3 To TRows
        If LCase(Worksheets("PNG Database").Cells(i, 1).Value) = LCase(TextBox1.Text) Then
                
            txtLastName.Text = Worksheets("PNG Database").Cells(i, 1).Value
            txtFirstName.Text = Worksheets("PNG Database").Cells(i, 2).Value
            txtC.Text = Worksheets("PNG Database").Cells(i, 3).Value
            txtNewCard.Text = Worksheets("PNG Database").Cells(i, 4).Value
            txtAccDes.Text = Worksheets("PNG Database").Cells(i, 5).Value
            txtAccCreated.Text = Worksheets("PNG Database").Cells(i, 6).Value
            txtAccGranted.Text = Worksheets("PNG Database").Cells(i, 7).Value
            txtAccCancelled.Text = Worksheets("PNG Database").Cells(i, 8).Value
            txtAccCategory.Text = Worksheets("PNG Database").Cells(i, 9).Value
            txtNotes.Text = Worksheets("PNG Database").Cells(i, 12).Value
         
            Exit For
        End If
    Next i
    
   


End Sub

Any help would be great. Thanks in advance.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Rather than accessing the sheet all the time, how about adding a listbox to your form, that can then be populated with the contents of textbox1.
That way you can see all instances of the last name in one go.
Code:
Option Explicit
Private Dic As Object

Private Sub ListBox1_Click()
   With Me.ListBox1
      Me.[COLOR=#ff0000]tb2[/COLOR].Value = .List(.ListIndex, 0)
      Me.[COLOR=#ff0000]tb3[/COLOR].Value = .List(.ListIndex, 1)
      Me.[COLOR=#ff0000]tb4[/COLOR].Value = .List(.ListIndex, 2)
   End With
End Sub

Private Sub TextBox1_AfterUpdate()
   Me.ListBox1.List = Dic(Me.TextBox1.Value).Value
End Sub

Private Sub UserForm_Initialize()
   Dim Cl As Range
   Dim ws As Worksheet

   Set ws = Sheets("PNG Database")
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = vbTextCompare
   For Each Cl In ws.Range("A3", ws.Range("A" & Rows.Count).End(xlUp))
      If Not Dic.exists(Cl.Value) Then
         Dic.Add Cl.Value, Cl.Resize(, 12)
      Else
         Set Dic(Cl.Value) = Union(Dic(Cl.Value), Cl.Resize(, 12))
      End If
   Next Cl
End Sub
Then when you click on a line in the listbox, it will populate your textboxes. Change values in red to match your textbox names
 
Upvote 0
Hi,
untested but see if this update to your code does what you want

Code:
Dim TRows As Long


Private Sub btnLName_Click()
    Static StartRow As Long




    Set PNGDatabase = ThisWorkbook.Worksheets("PNG Database")


    If StartRow = 0 Then StartRow = 3
    With PNGDatabase
    TRows = .Range("A1").CurrentRegion.Rows.Count
    For i = StartRow To TRows
        If LCase(.Cells(i, 1).Value) = LCase(TextBox1.Text) Then
            txtLastName.Text = .Cells(i, 1).Value
            txtFirstName.Text = .Cells(i, 2).Value
            txtC.Text = .Cells(i, 3).Value
            txtNewCard.Text = .Cells(i, 4).Value
            txtAccDes.Text = .Cells(i, 5).Value
            txtAccCreated.Text = .Cells(i, 6).Value
            txtAccGranted.Text = .Cells(i, 7).Value
            txtAccCancelled.Text = .Cells(i, 8).Value
            txtAccCategory.Text = .Cells(i, 9).Value
            txtNotes.Text = .Cells(i, 12).Value
            StartRow = i + 1
            Exit Sub
        End If
    Next i
    End With
    StartRow = 0
    MsgBox "Last Record Found", 48, "End Of File"
    
End Sub

Each press of the button should display next occurrence of search record until msgbox displayed

Dave
 
Last edited:
Upvote 0
Fluff,
I'm getting a Compile Error: Sub or Function not defined here:

Private Sub TextBox1_AfterUpdate()
Me.ListBox1.List = Dic(Me.TextBox1.Value).Value
End Sub
 
Upvote 0
The first two lines of code ie
Code:
Option Explicit
Private Dic As Object
must go at the very top of the userform module, before any code.
 
Upvote 0
Fluff,
I really appreciate you walking me through this but now I'm getting run time error 91: Object variable or with block variable not set here :

Private Sub TextBox1_AfterUpdate()


Me.ListBox1.List = Dic(Me.TextBox1.Value).Value
End Sub
 
Upvote 0
Have you created a listbox? If so, is it called ListBox1?
 
Upvote 0
No it needs to be on the userform.
Have a look at this file & see if it works for you
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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