Consulting

Results 1 to 6 of 6

Thread: Excel 2013>VBA>UserForm>TextBox>Name as variable

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location

    Excel 2013>VBA>UserForm>TextBox>Name as variable

    Hello,

    I'm trying to call the name of a textbox on my userform from a variable. Is that possible?

    I know that my variable is working correctly. And I know that the value I want to place in the textbox is working correctly. I just can't get the textbox name from the variable.

    Here's the code:

    Private Sub CommandButton5_Click()
     
     Dim boxSMARTdest As String
     Dim dest As MSForms.TextBox
     
     boxSMARTdest = frmSMARTNumberPicker.bxSMRTvalue.Caption
     dest = Forms("frmFinishTA").Controls(boxSMARTdest)
     
     With dest
      .Text = frmSMARTNumberPicker.Label1.Caption
     End With
     
     Unload Me
     
    End Sub

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I'm pretty sure that the Forms("frmFinishTA") is a syntax error.
    I'm very sure that you need to use the keyword Set when assigning the Textbox variable, dest.

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    mikerickson,

    Thanks for responding.

    How would I use the keyword Set when assigning the Textbox variable, dest? I'm not familiar with that.

  4. #4
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    I reread your post after I had more time...I understand what you're saying now! Ha.

  5. #5
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    Here's what I got to work:

    Private Sub CommandButton5_Click()
         
        Dim boxSMARTdest As String
        Dim dest As MSForms.TextBox
         
        boxSMARTdest = frmSMARTNumberPicker.bxSMRTvalue.Caption
        Set dest = frmFinishTA.Controls(boxSMARTdest)
         
        With dest
            .Text = frmSMARTNumberPicker.Label1.Caption
        End With
         
        Unload Me
         
    End Sub

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Now you've developed it, shorten it (untested):
    Private Sub CommandButton5_Click()
    frmFinishTA.Controls(frmSMARTNumberPicker.bxSMRTvalue.Caption).Text = frmSMARTNumberPicker.Label1.Caption
    Unload Me
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •