Search code examples
excelvbaformsautofill

How to autofill the excel form based on the cell in the active worksheet using VBA?


enter image description here

I created an Excel form and expect the textbox in the form able to be auto-filled by the value in cell B2. I had tried but seems like my code in VBA didn't work for me. I attached my code below.

Private Sub UserForm_Click()

    With UserForm1
        .txtTextBox1.Value = Sheet7.Range("B10").Value
        
    End With


End Sub

Solution

  • Use form Initialize event like below.

    Private Sub UserForm_Initialize()
        Me.TextBox1 = Sheets("Sheet1").Range("B10") 'Change sheet1 to your sheet name.
    End Sub
    

    enter image description here