VBA ListBox .AddItem - Line Spacing

sthrncaliguy

Board Regular
Joined
Jul 28, 2009
Messages
213
I have a quick question..

I am populating a listbox via .additem, but when I do this there are several blank lines inserted between each item. I am not sure why this is the case.

I am adding items via a combobox..

So when the user changes a combobox, the onchange code reads:

With myListBox
.additem myCboBox.Value
end with
myCboBox.Value = ""

I am not sure why there is such a gap between each added item, but would like to know if there is a way around this... I didn't see any formatting options in the listbox properties that looked like they related to this behavior..
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hello ,

The problem is the ComboBox event you are using. The Change event, which is the default event, is triggered a second time by the statement:
Code:
    myCboBox.Value = ""
This adds in the blank line. To eliminate this, you need to move your code to the ComboBox_Click event procedure. Copy this code and paste it into your UserForm and then delete the old Change event code.
Code:
Private Sub CboBox_Click()
    With myListBox
      .AddItem myCboBox.Value
    End With
    myCboBox.Value = ""
End Sub
Sincerely,
Leith Ross
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top