Forum Discussion

aekbus's avatar
aekbus
Brass Contributor
Dec 09, 2025

Initializing TextBoxes within a UserForm

My spreadsheet contains a macro which utilizes two UserForms, each containing several TextBoxes.  I want to initialize several TextBoxes in each Userform.

One UserForm works and the other one does not, even though I have set the code up identically in each one.  Here is what I have detected:  When you step through the macro code (F8), when the first UserForm.Show statement is executed, the control skips to the first line of code for the UserForm and when the second UserForm.Show is executed, the control skips to the first TextBox on the Object.

Here is a sample of the code I am using:

Private Sub UserForm1()

   TextBox1.Value = "ABCDE"

End Sub

I have been writing macros like this for over 30 years, but this is the first time I have run into this situation.  Any help here will be greatly appreciated.

Al

 

3 Replies

  • Harun24HR's avatar
    Harun24HR
    Bronze Contributor

    How you are executing the codes? I means which event? This works fine to me with UserForm_Activate event.

    Private Sub UserForm_Activate()
        Me.TextBox1.Value = "MyValue"
    End Sub

     

    • aek_17's avatar
      aek_17
      Copper Contributor

      I have tried to use the reply you gave me, but I was not successful.

      I have reduced my problem into the simplest of terms to illustrate my problem, but I don't know how to attach the file to this reply.  Give me your email address and I can send it to you.

      I tried your solution again and this time it worked, and I'll use it.  I still don't know why my method failed because I have been using it for years.   Anyway, thanks.

Resources