Dynamically Adding Labels to User Form = Blank UserForm

后端 未结 3 2007
礼貌的吻别
礼貌的吻别 2020-12-31 06:10

I\'m trying to dynamically add buttons to the userform, but the userform just comes up blank. Ive simplified the essence of the code as much as possible for error checking (

相关标签:
3条回答
  • 2020-12-31 06:48

    A few things:

    1. You need to show your UserForm as vbModeless - else the code stops on UserForm2.Show
    2. You are creating an object called Label then using With on theLabel
    3. You will then need to increment the position of your three labels to avoid overlap (which I have done using Top).

      Sub addLabel()
      UserForm2.Show vbModeless
      Dim theLabel As Object
      Dim labelCounter As Long
      
      For labelCounter = 1 To 3
          Set theLabel = UserForm2.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
          With theLabel
              .Caption = "Test" & labelCounter
              .Left = 10
              .Width = 50
              .Top = 10 * labelCounter
          End With
      Next
      End Sub
      
    0 讨论(0)
  • 2020-12-31 06:53

    try below code

    Set theLabel = UserForm2.Designer.Controls.Add("Forms.Label.1", "Test1", True)
    
    0 讨论(0)
  • 2020-12-31 07:02

    After the end with statement, add:

    userform1.show
    

    One more correction:

    .top = 10*labelcounter+10
    
    0 讨论(0)
提交回复
热议问题