Pulling info from a Sheet into UserForm

Dan Swartz

Board Regular
Joined
Apr 17, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with a customer database listing my customers. I have a user form on a different page to look for a customer in the customer database, and if it's there, it will fill out the form box with the information that is listed in the Customer database. once the info has been populated to the user form and I can change the information and have it right back to the database with the changes. (This code is working fine. I won't include it) Everything works as expected, but only for the first name in the database. It can't find anyone below the first name listed. it says "Customer does not exist" which is what it's supposed to do if it doesn't exist. However, it does exist and I'm sure it's something little that I'm missing. I'm new to vba. So be nice! :)

VBA Code:
Private Sub FindCust_Click()
    
    Dim CustN As String
 
    CustN = CustomerName.Text
    
    lastrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 3 To lastrow
        If Sheet2.Cells(i, 1).Value = CustN Then
            Address1.Text = Sheet2.Cells(i, 2).Value
            Address2.Text = Sheet2.Cells(i, 3).Value
                City.Text = Sheet2.Cells(i, 4).Value
               State.Text = Sheet2.Cells(i, 5).Value
                 Zip.Text = Sheet2.Cells(i, 6).Value
               Email.Text = Sheet2.Cells(i, 7).Value
            Exit Sub
        
        Else
            MsgBox ("Customer does not exist"), vbOKOnly
            
            CustomerName.SetFocus
            
            Exit Sub
                        
    End If
            
Next
      
End Sub
 

Attachments

  • Update Customer Form.jpg
    Update Customer Form.jpg
    50.7 KB · Views: 7

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try the Find method:

VBA Code:
Private Sub FindCust_Click()
  Dim f As Range
  
  If CustomerName.Value = "" Then
    MsgBox "Enter name"
    CustomerName.SetFocus
    Exit Sub
  End If
  
  Set f = Sheet2.Range("A:A").Find(CustomerName.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    Address1.Text = Sheet2.Cells(f.Row, 2).Value
    Address2.Text = Sheet2.Cells(f.Row, 3).Value
    City.Text = Sheet2.Cells(f.Row, 4).Value
    State.Text = Sheet2.Cells(f.Row, 5).Value
    Zip.Text = Sheet2.Cells(f.Row, 6).Value
    Email.Text = Sheet2.Cells(f.Row, 7).Value
  Else
    MsgBox ("Customer does not exist"), vbOKOnly
    CustomerName.SetFocus
  End If
End Sub
 
Upvote 0
Solution
Try the Find method:

VBA Code:
Private Sub FindCust_Click()
  Dim f As Range
 
  If CustomerName.Value = "" Then
    MsgBox "Enter name"
    CustomerName.SetFocus
    Exit Sub
  End If
 
  Set f = Sheet2.Range("A:A").Find(CustomerName.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    Address1.Text = Sheet2.Cells(f.Row, 2).Value
    Address2.Text = Sheet2.Cells(f.Row, 3).Value
    City.Text = Sheet2.Cells(f.Row, 4).Value
    State.Text = Sheet2.Cells(f.Row, 5).Value
    Zip.Text = Sheet2.Cells(f.Row, 6).Value
    Email.Text = Sheet2.Cells(f.Row, 7).Value
  Else
    MsgBox ("Customer does not exist"), vbOKOnly
    CustomerName.SetFocus
  End If
End Sub
This worked great! thank you for the answer
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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