Autofill Userform TextBox with data on sheets

liaaa

New Member
Joined
Sep 29, 2023
Messages
20
Office Version
  1. 2010
Platform
  1. Windows
Good morning,
For Project Name and No Contract I want it autofill based on NIP that I have choose. This is my code and its not working

Private Sub CmbNIP_Change()
Dim i As Long, LastRow As Long, ws As Worksheet
Set ws = Sheets("List Proyek")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow
If Val(Me.CmbNIP.Value) = ws.Cells(i, "A") Then
MsgBox Me.CmbNIP.Value
Me.txtPROJECTNAME = ws.Cells(i, "B").Value
Me.txtNOCONTRACT = ws.Cells(i, "C").Value
End If
Next i
End Sub


This is the data on sheet List Proyek
1696042625860.png


Thank you for your help
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi

Welcome to forum

Looking at your data, you are searching for an Alpha Numeric value in the Column yet you have wrapped your combobox in the VAL function which will stop reading your search string at the first character that it does not recognize as part of a number. Also, your code is searching values case sensitive & maybe better to change this to make it case insensitive.

See if this update to your code now does what you want

Code:
Private Sub CmbNIP_Change()
    Dim i           As Long, LastRow As Long
    Dim ws          As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("List Proyek")
    LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRow
        If UCase(Me.CmbNIP.Value) = UCase(ws.Cells(i, "A").Value) Then
            Me.txtPROJECTNAME = ws.Cells(i, "B").Text
            Me.txtNOCONTRACT = ws.Cells(i, "C").Text
        End If
    Next i
End Sub

Note: that I have used the Range.Text property to return the search result to the controls. Using this property returns what you see in the cell (its format like a date or currency) rather than its underlying value.

Another observation it looks like you are using an Excel Table yet writing code for a Range.

Tables are powerful tools & make things much easier in so many ways especially, when writing code to access the data.

Here is one guide that you may find helpful in this respect: VBA Tables and ListObjects - Excel Off The Grid

Finally for future, when sharing data here, forum will find it more helpful if you use MRExcel Addin XL2BB - Excel Range to BBCode
to post copy of your worksheet rather than an image.



Dave
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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