Can't change dimensions of dynamically added listbox - vba

I have been looking for hours now and I hope someone can help me here.
I am creating a form in vba where I want to dynamically add Listboxes. The listboxes are added, so that goes as planned. However, I can't seem to change the width and heigth of the listbox. All other things are being changed. This is my code:
Dim lb As MSForms.ListBox
Set lb = Me.controls.add("Forms.Listbox.1")
With lb
.ColumnCount = 4
.Left = 180
.Top = 16
.MultiSelect = 1
.Height = 270
.Width = 665
End with
The other thing that I noticed is that Heigth and Width aren't in the property list of the ListBox. Could it be that I use the wrong ListBox type?
EDIT: It get's stranger. When I go through the code line by line, it functions normally and the ListBox size is as I want it.

Try this may be
Me.Controls("Forms.Listbox.1").IntegralHeight = False
or
lb.IntegralHeight = False

Related

VBA - How to resize a VBA UserForm based on its contents

Here is my problem:
I have a simple VBA UserForm and a Label (Label1) inside it which I called it u1_Status to inform users to what is happening behind the processes of my code. Its label will have different text at different stages of my code.
I want my UserForm size change according to the label inside it. For example when I have long informative text in Label1 I want my UserForm size increases and if label1 text in later stages is short my UserForm fits its dimensions to this updates text.
Here is my simplified code so far:
sub Test1()
Dim htLabelHeight: htLabelHeight = u1_Status.Label1.Height
Dim wdLabelWidth: wdLabelWidth = u1_Status.Label1.Width
u1_Status.Height = htLabelHeight
u1_Status.Width = wdLabelWidth
u1_Status.Show
end sub
The problem is that the width is Ok but the height seems to be Zero.
How my userform should looks:
How it Looks with my code:
The height of the userform also includes the height of the title bar. So you have to use the (read only) InsideHeight property of the userform.
Option Explicit
Private Sub UserForm_Activate()
With Me
Dim titleBarHeight As Long
titleBarHeight = .Height - .InsideHeight
.Label1.Caption = Sheet1.Range("A10")
.Height = .Label1.Height + titleBarHeight
.Width = .Label1.Width
End With
End Sub

Adding multiple userform controls at runtime

I have created a userform with a static label to instruct the user to check certain part numbers. The form will also have a static button to submit the input. However, I need the checkboxes to be made at runtime since it won't be certain how many checkboxes there will be every time the form is shown. There would be a max of twelve boxes, but most of the time only one or two boxes would be needed and I'd prefer to show only what's needed instead of having blank checkboxes. I've made a for loop to add the boxes.
Currently I can add one checkbox to the form, but in the record I'm working with there are supposed to be three. Everything that I am seeing online about dynamically adding controls mainly pertains to adding events to the controls, but I don't need events, just the controls. Can anyone see what I need to add/change in my code:
Private WithEvents chkbox As MSForms.CheckBox
Private Sub UserForm_Activate()
For i = 1 To 12
If PNArray(i) Like "A*" And ChangeArray(i) = "New Part Number:" Then
With Me.Controls
Set chkbox = .Add("Forms.checkbox.1", "chkbox" & i, True)
With chkbox
.Width = 108
.Height = 18
.Top = 30 * i
.Left = 54
.Caption = PNArray(i)
End With
End With
End If
Next i
End Sub
Thanks
After some researching, the problem is .Top=30*i. You multiply always by index of array, when you want all checkboxes same distance. You need another variable to do that. Try like this:
Dim ZZ As Byte ' count of checkboxes
ZZ = 0
For i = 1 To 12
If PNArray(i) Like "A*" And ChangeArray(i) = "New Part Number:" Then
ZZ = ZZ + 1 'we increase count of checkboxes
With Me.Controls
Set chkbox = .Add("Forms.checkbox.1", "chkbox" & i, True)
With chkbox
.Width = 108
.Height = 18
.Top = 30 * ZZ 'we position the chekbox according to its rank (first one, second one, third one, and so on).
.Left = 54
.Caption = PNArray(i)
End With
End With
End If
Next i
With this code, it does not matter how many checkboxes you have. It will add them in the right position, in a "podium" style

Excel VBA Change Height and width of ListBox Programatically

Sub add_ListBox()
Dim box As msforms.ListBox
Dim myBox As Object
For i = 0 To Select_Files.FileBox1.ListCount - 1
Set box = UserForm4.Controls.Add("Forms.ListBox.1", "tSourceBox" & i + 1, True)
Set myBox = box
With myBox
.ColumnCount = 2
.ColumnWidths = "0 pt;189 pt"
.IntegralHeight = True
.Top = 24
.Left = 6
.Height = 153
.Width = 189
End With
Next
End Sub
The above code suddenly stopped working properly, and I don't understand why. Initially, it was producing the ListBoxes with the specified height and width. However, all of a sudden when I run the code height and width are not what is specified in the code.
How can I make it so that I have control over the height and width of the ListBoxes?
Excel 2010 Windows 7 x64
There is a ListBox property called IntegralHeight for ActiveX and Forms. Set it to False.

Control Caption Text is displayed smaller

I'm working on a Userform in Excel that has to be dynamically generated each time. It can list many (100+) lines which are all exactly the same in format. These are generated by the following code snippet.
' ctextbox
Set ctl = .Controls.Add("Forms.Textbox.1")
With ctl
.Top = 12 + linetop
.Left = 464.9
.Width = 140
.Height = 18
.Name = FieldName & "_ctextbox"
End With
' cshow
Set ctl = .Controls.Add("Forms.CommandButton.1")
With ctl
.Top = 13.1 + linetop
.Left = 611.35
.Width = 41.95
.Height = 18
.Name = FieldName & "_cshow"
.Caption = "Show All"
End With
' confirm
Set ctl = .Controls.Add("Forms.Checkbox.1")
With ctl
.Top = 13.5 + linetop
.Left = 659
.Width = 44.95
.Height = 17.25
.Name = FieldName & "_confirm"
.Caption = "Confirm"
End With
It would fine except for a random occurrence where the Confirm checkbox appears smaller than the rest. The screenshot below shows what I mean.
Has anyone experienced this issue before?
I would recommend using repainting the Userform after you have added the controls dynamically.
The Repaint method completes any pending screen updates for a specified form. When performed on a form, the Repaint method also completes any pending recalculations of the form's controls.
This method is useful if the contents or appearance of an object changes and you don't want to wait until the system automatically repaints the area. Me.Repaint simply updates the display by redrawing the form
I had the same issue in that my repaint did not work. I solved this by setting the CheckBox AutoSize property to True and I have no problems anymore.

The name of a frame in a loop

I want to add a Control in every for in my application. Let's say I have 5 frames...
I want to do someting like this:
Set cControl = Me!iooly&i.Controls.Add("Forms.Label.1", "str12" & i, True)
With cControl
.Caption = "1/2"
.Width = 20
.Height = 8
.Top = 10
.Left = 435
End With
i is a counting variable
the problems is that Me!iooly&i ... Can I do this operation when my frames have names la iooly1, iooly2, iooly3 and so on?
Your Me is presumably a form? This won’t work. Also, the Me!iooly&i syntax doesn’t work, this only works if your string is a constant.
You can use the Forms collection though:
Set cControl = Forms("iooly" & i).Controls.Add(…)
This is assuming that the form already exists. If it doesn’t, you need to first load it.