Userform listbox - Height

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
How can you find the default height of listbox entries.

This is to resize the height based on the formula:
Height = Row height * number of entries.

Can it be done in pixels as well?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think, by default, (at least in my installation) MsForms creates listboxes with a height of 72 pts, with 7 entries and with the IntegralHeight set to TRUE.
So, in theory, this should give you the default height of a single listbox entry:
DefHeight= 72/7

To convert to pixels just multiply by 0.75

Alternatively, you could use Microsoft Active Accessibillity AccLocation Method to read the height of a single entry. This method is probably more accurate.
 
Upvote 0
Actually, after removing the listbox border , it gives a listbox height of 68.25 pts. This is more accurate.

So the default height of a listbox entry should be : (68.25/7) = 9.75 points
 
Upvote 0
Thanks for assisting.
I'll look into AccLocation and use 9.75 in the meantime.
 
Upvote 0
Here is a generic SUB that will adjust the height of the listbox to the number of entries of your choice while taking into account the listbox border style.

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#Else
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
#End If

Private Sub SetHeight(ByVal LBox As MsForms.ListBox, ByVal NumberOfEntries As Long)
    Const SM_CYEDGE = 46&
    With LBox
        NumberOfEntries = IIf(NumberOfEntries > .ListCount, .ListCount, NumberOfEntries)
        .Height = ((9.75 * NumberOfEntries) + IIf(.SpecialEffect = fmSpecialEffectFlat, 0, GetSystemMetrics(SM_CYEDGE)))
    End With
End Sub

Usage:
So, for example, the following will resize the listbox to 10 entries :
VBA Code:
SetHeight ListBox1, 10
 
Upvote 0
Solution
Thanks Jaafar, this is quite useful.
Thanks for the feedback.
To convert to pixels, just multiply by 0.75
Ooops ! That was a mistake in post#2 regarding the conversion to pixels... In case you still want to convert the default row height to pixels, it should be multiplied by 1/0.75
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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