Consulting

Results 1 to 3 of 3

Thread: Align List Boxes On User Form Using a Loop

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location

    Align List Boxes On User Form Using a Loop

    Good afternoon, I am looking for some assistance in aligning list boxes on a userform. I have 17 list boxes and am using a loop to align them next to each other. Unfortunately I cant get listbox1 lined up next to listbox2 and I can't figure out why. Listboxes 2 thru 17 line up next to each other as intended. Below is the code I am using to accomplish this.

    Private Sub UserForm_Activate()
    Dim ListBXheight  As Long
    Dim ListBXleft    As Long
    Dim UFh           As Long
    Dim CmdOKtop        As Long
    Dim Ctrl          As control
    
    ListBXheight = 650
    ListBXleft = 8
    CmdOKtop = 700
    
    'Size and Move the user form
    With EntryKey2
        .Height = 750
        .Width = 1100
        .Left = 50
        .Top = 10
      '  .Font.Size = 20
    End With
    
    'Size and Move the listboxes
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "ListBox" Then
           With Ctrl
             .Height = ListBXheight
             .Width = 50
             .Left = ListBXleft
             .Top = 5
          End With
        End If
        ListBXleft = ListBXleft + 50
    Next Ctrl
    
    'Size and Move the user form "OK" button
    With Me.CmdOK
       .Height = 25
       .Width = 100
       .Left = 55
       .Top = CmdOKtop
    End With
    With Me.CmdPrint
       .Height = 25
       .Width = 100
       .Left = 400
       .Top = CmdOKtop
    End With
        
    End Sub
    I have also attached a short version of the workbook that I am using that will demonstrate it.
    Thanks in advance.
    Gary
    Attached Files Attached Files

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Here, give this a go. The reason you failed is (I think), when you reposition your LBs, they are "all over the place". You need to sequence them. What I did was resequence your TabIndex then cycle them.
    Attached Files Attached Files

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    323
    Location
    Thx JKwan, your code worked. I was not aware that a listbox can be identified by TabIndex. Thanks for the tip.

    Gary

Posting Permissions

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