1

I created a form in VBA where I dynamically add ListBoxes.

How do I change the width and height of the ListBox? All other things are being changed.

    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

I noticed Height and Width aren't in the property list of the ListBox. Could it be that I use the wrong ListBox type?

When I go through the code line by line, the ListBox size is as I want it.

4
  • The code looks good. I've created a form that takes height and width from a couple of combo boxes and sends these dimensions to an edited copy of your code. Have you tried adding Me.Repaint after an update?
    – MiguelH
    Commented Aug 28, 2015 at 9:50
  • Me.Repaint didn't work, however going trough the code line by line seems to work. Commented Aug 28, 2015 at 10:33
  • Is there anything else that happens after the ListBox creation?
    – MiguelH
    Commented Aug 28, 2015 at 11:03
  • I found the solution: After the creation, Items are added to the listboxes and I have set the heigth and width again after adding the item. This seems to work, but I still don't understand why it worked when going through the code line by line. Commented Aug 28, 2015 at 13:57

2 Answers 2

0

Try this may be

Me.Controls("Forms.Listbox.1").IntegralHeight = False or lb.IntegralHeight = False

0

In my Word VBA script I dynamically added a ListBox item today and encountered the same error. The .Height and .Width property seem to be missing and the ListBox doesn't get resized when setting a custom height or width. After a lot of troubleshooting I found out that the MultiSelect property is blocking the resizing if it is set to fmMultiSelectMulti or fmMultiSelectExtended.

So, I case anyone else runs into the problem:

FIRST set the width and the height and THEN AFTERWARDS set the MultiSelect property.

Couldn't believe that something like this is the problem...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.