Change the line spacing between items in a VBA listbox

KeepTrying

Active Member
Joined
Aug 19, 2012
Messages
276
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi Guys,

I'd like to change the line spacing between items in a VBA listbox. Let's assume I display month names in a listbox control. Name of my listbox is "Lista". It's easy, I just add one line to "Private Sub UserForm_Initialize" for the userform:
Code:
Lista.List = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")


But I want more spaces between month names in the listbox when it shows on the userform. How Can I do that?

Many thanks in advance for your help.

Kind Regards,
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this:-
NB:- Change variable "Sp" (spaces required) in code to suit.
Code:
Private [COLOR="Navy"]Sub[/COLOR] UserForm_Initialize()
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] sp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
sp = 6 '[COLOR="Green"][B] Enter Blank Rows between month, in Listbox here !!![/B][/COLOR]
[COLOR="Navy"]With[/COLOR] Me.ListBox1
  .Clear
[COLOR="Navy"]For[/COLOR] n = sp To sp * 12 + (sp - 1)
    [COLOR="Navy"]If[/COLOR] n Mod sp = 0 [COLOR="Navy"]Then[/COLOR]
       c = c + 1
        .AddItem MonthName(c)
    [COLOR="Navy"]Else[/COLOR]
        .AddItem " "
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Dear Mick,

Many thanks for your quick help, code works great. However giving value 2 to "sp" takes too much space between month names (value 1 is the original spacing). So I'd need a value between 1 and 2 (let's say 1.5). Of course I cannot provide fractional number to "sp" (even if I change its declaration from long to double) because for MOD a fraction portion is just truncated. Do you have any idea how to achieve this?

Regards,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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