View Single Post
  #18   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default ComboBox listwidth to match text in list

Just a thought, but why change your settings on each turn of the loop?
Seems like it would be faster to get the values and set them once after the
loop is completed.

--
Regards,
Tom Ogilvy


"J.S.Winstrom" wrote:

"J.S.Winstrom" wrote in
8.16 on Thu 02 Aug
2007 11:32:12a:

I am having some trouble in finding information on how to set the
listwidth/columnwidths in a combobox to match the text longest entry.
Any help would be greatly apreciated!

~J.S.Winstrom


I found a method that worked. As suggested by Jim, I used a factor of 5
and here's the code that I came up with to dynamicaly set the .ListWidth
and .ColumnWidths:

(getCount is already declared as a Public variable because I use it
everywhere and this rountine is nested within the Userform_Activate()
routine after the commands that fetch data from am Access database and
populate the list)

__________________________________________________ _____

Dim col1Width, col2Width, c1Width, c2Width, iNum
getCount = .ListCount - 1
c1Width = Len(.List(getCount, 0)) * 5
c2Width = Len(.List(getCount, 1)) * 5
For iNum = 0 To getCount
col1Width = Len(.List(iNum, 0)) * 5
col2Width = Len(.List(iNum, 1)) * 5
If col1Width c1Width Then
If col2Width c2Width Then
.ColumnWidths = col1Width & ";" & col2Width
Else
.ColumnWidths = col1Width & ";" & c2Width
End If
ElseIf col1Width < c1Width Then
If col2Width c2Width Then
.ColumnWidths = c1Width & ";" & col2Width
Else
.ColumnWidths = c1Width & ";" & c2Width
End If
End If

If Val(col1Width + col2Width) Val(c1Width + c2Width) Then
.ListWidth = Val(col1Width + col2Width)
ElseIf Val(c1Width + col1Width) Val(c2Width + col2Width) Then
.ListWidth = Val(c1Width + col1Width)
ElseIf Val(c1Width + col2Width) Val(col1Width + c2Width) Then
.ListWidth = Val(c1Width + col2Width)
ElseIf Val(col1Width + col2Width) < Val(c1Width + c2Width) Then
.ListWidth = Val(col1Width + col2Width)
ElseIf Val(c1Width + col1Width) < Val(c2Width + col2Width) Then
.ListWidth = Val(c1Width + col1Width)
ElseIf Val(c1Width + col2Width) < Val(col1Width + c2Width) Then
.ListWidth = Val(c1Width + col2Width)
End If
If iNum = getCount Then Exit For
Next iNum