Results 1 to 2 of 2

Thread: Adding controls at design time through code

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Adding controls at design time through code

    The following code adds Labels to Userform1. However all the Labels appear on top of each other at the top left of the userform. What might the problem be?

    Code:
    Sub AddControls()
      Dim UFvbc As VBComponent
      Dim c, lb As Control
      Dim r As Long
      Dim x As VBIDE.VBProject
     
      
    '   Make sure access to the VBProject is allowed
        On Error Resume Next
        Set x = Workbooks(1).VBProject
        If Err <> 0 Then
            MsgBox "Your security settings do not allow this macro to run.", vbCritical
            Exit Sub
        End If
      
      Set UFvbc = x.VBComponents("Userform1")
    
    ' Delete all controls, if any
      For Each c In UFvbc.Designer.Controls
        UFvbc.Designer.Controls.Remove c.Name
      Next c
    
    ' Add Labels
      For r = 1 To 10
          Set lb = UFvbc.Designer.Controls.Add("Forms.label.1")
          With lb
            .Width = 25
            .Height = 25
            .Left = 10
            .Top = (r * .Height) + 35
            .Caption = Worksheets(3).Cells(r, 1)
            .Tag = "ItemNo " & r
            .Name = "lbItemNo" & r
          End With
          
    'Initialise the userform
    VBA.UserForms.Add ("Userform1")
    
    End Sub
    Thanks for your help
    Last edited by UpSkill; 01-30-2014 at 04:54 PM.

Similar Threads

  1. Adding function without messing up original code
    By peter renton in forum Excel Help
    Replies: 5
    Last Post: 12-24-2013, 01:15 PM
  2. Replies: 2
    Last Post: 10-11-2013, 04:46 AM
  3. adding entries into combobox with code
    By paul_pearson in forum Excel Help
    Replies: 1
    Last Post: 07-23-2013, 01:01 PM
  4. Office Version Independent Non-Activex Date Time Picker Using Form Controls
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 07-17-2013, 12:27 AM
  5. Adding charts via code to a protected sheet
    By Rasm in forum Excel Help
    Replies: 2
    Last Post: 11-14-2012, 05:11 PM

Posting Permissions

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