简体   繁体   中英

VBA Excel - Show row data in the UserForm based on the ID

I'm currently working with an UserForm with an UPDATE function. I just want to ask if how can I show the value in my UserForm? As of now, I only accomplished it by using the code below.

txtBox_ID.Text = CStr(ThisWorkbook.Sheets("Results").Range("D7").Value)
txtBox_lname.Text = CStr(ThisWorkbook.Sheets("Results").Range("D8").Value)
txtBox_fname.Text = CStr(ThisWorkbook.Sheets("Results").Range("D9").Value)
txtBox_mname.Text = CStr(ThisWorkbook.Sheets("Results").Range("J8").Value)
txtBox_ext.Text = CStr(ThisWorkbook.Sheets("Results").Range("J9").Value)

As you can see the data is coming from Results sheet, but I want the UserForm to get the data in the Data sheet in which all the other data are located since I think it is needed in the find and update process.

Here's the sample file so you can understand better. I'm just a newbie in this VBA thing and sorry for the poor English.

This seems to work on my worksheet.

Private Sub UserForm_Initialize()

    Set ws = ActiveWorkbook.Worksheets("Data")
    lastrow = ws.Cells(Rows.Count, "C").End(xlUp).Row
    
    For r = 10 To lastrow
    If ws.Cells(r, 3) = CStr(ThisWorkbook.Sheets("Results").Range("D7").Value) Then
        txtBox_ID.Text = ws.Cells(r, 3).Value
        txtBox_lname.Text = ws.Cells(r, 4).Value
        txtBox_fname.Text = ws.Cells(r, 5).Value
        txtBox_ext.Text = ws.Cells(r, 6).Value
        txtBox_mname.Text = ws.Cells(r, 7).Value
    End If
    
    Next
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM