none
Is it possible to have one label that is visible in multiple userforms in VBA? RRS feed

  • Question

  • Hi Guys,

    I'm new to using VBA and coding but am developing a learning tool for work. What I need to find out is if it's possible to create a single label and then for that label to be seen in multiple userforms?

    The issue with that is I have to copy and paste all the labels into every userform. That's okay, but it's not efficient!


    I'd like to know whether there's a way you can show labels or buttons for any userform, but with only having one copy of them.
    I just tried putting their info in a module and then calling the procedure within that module but that didn't work because there's nowhere stated that they centrally belong.
    The idea is for all userforms to be able to show these labels that are not inherently part of that userform.

    Thanks for any help!

     

    Wednesday, June 10, 2015 12:26 PM

Answers

  • Chris,

      You can try and use this as I had a similar need but only had one form to deal with.  Just make sure the labels and textboxes start at 1 and are in the order that you want and change the numbers in i to suit your form.  Just change the worksheet name to your on.

    HTH

    Harry

    Private Sub UserForm_Initialize()
    'Use this code to replace label
    'modify to add tag to each text field
    'Subtract 2 from label to offset start at row 3
    Dim i As Integer
    For i = 4 To 25
        Me.Controls("label" & i - 2).Caption = Worksheets("Bank Acct").Range("O" & i)
        Me.Controls("textbox" & i - 2).Tag = Worksheets("Bank Acct").Range("O" & i)
    Next
    
    End Sub

    Wednesday, June 10, 2015 12:57 PM
  • Hi Chris,

    >>What I need to find out is if it's possible to create a single label and then for that label to be seen in multiple userforms?<<

    No, it is impossible. If the lables may change later, we can declare a variable and initialize the label with it. Or we also can refer to the code by Harry, initialize the label from worksheet.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 11, 2015 5:36 AM
    Moderator

All replies

  • Chris,

      You can try and use this as I had a similar need but only had one form to deal with.  Just make sure the labels and textboxes start at 1 and are in the order that you want and change the numbers in i to suit your form.  Just change the worksheet name to your on.

    HTH

    Harry

    Private Sub UserForm_Initialize()
    'Use this code to replace label
    'modify to add tag to each text field
    'Subtract 2 from label to offset start at row 3
    Dim i As Integer
    For i = 4 To 25
        Me.Controls("label" & i - 2).Caption = Worksheets("Bank Acct").Range("O" & i)
        Me.Controls("textbox" & i - 2).Tag = Worksheets("Bank Acct").Range("O" & i)
    Next
    
    End Sub

    Wednesday, June 10, 2015 12:57 PM
  • Hi Chris,

    >>What I need to find out is if it's possible to create a single label and then for that label to be seen in multiple userforms?<<

    No, it is impossible. If the lables may change later, we can declare a variable and initialize the label with it. Or we also can refer to the code by Harry, initialize the label from worksheet.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 11, 2015 5:36 AM
    Moderator