PDA

View Full Version : [SOLVED] Align List Boxes On User Form Using a Loop



zoom38
01-16-2018, 11:59 AM
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

JKwan
01-16-2018, 01:08 PM
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.

zoom38
01-16-2018, 02:16 PM
Thx JKwan, your code worked. I was not aware that a listbox can be identified by TabIndex. Thanks for the tip.

Gary