Adjust listbox to display string longer than listbox width using Excel VBA - excel

In my listbox the strings are longer than the listbox width.
I cannot change the listbox width because of the already made up surface design:
Is there a VBA property for the listbox to:
break the string in half and display it on more than one line
or
scroll horizontally, to see the whole string
If there's not that kind of property, I'd like to know of other ideas.

The ListBox is single line only and does not support line breaks.
If it is an ActiveX then it has a property called ColumnWidths. Set it to something bigger like 500 and you will see a scrollbar at the bottom of the ListBox.

Related

Removing the scroll bar on a list box in Excel VBA UserForms

I'm using a UserForm in Excel VBA and I have a combo box with style fmStyleDropDownList. I want to remove the horizontal scrollbar:
Picture of my combo box with ColumnWidths of 100 pt. The combo box shown has a Width property of 162, a ColumnWidths property of 100 Pt and a ListWidth property of 162 Pt.
Keep in mind that this problem is somewhat similar to this one:
Remove the scroll bar on a list box. In this post, the accepted solution states:
This can occur when you have the column width format property of the listbox set to something wide, then you size the control to the data, it will show the scroll bars.
To fix this, change the "Column Width" property under the format tab to something really small, then play around with this property and the size of the listbox control until you get it how you want, and no scroll bars are displayed.
So what I did is: I changed the ColumnWidths property to smaller values until the horizontal scroll bar disappears. However, this has the side effect that the drop down text has not enough width to be displayed as a whole as it is shown in the following Pictures (the Width and LastWidth property remain at 162):
Picture of my combo box with ColumnWidths of 50 pt. The scroll bar is still there. Instead of "abcdefghijklmn", only "abcdefghijkl" (without mn) is dislayed in the second row of the drop down menu and in the third row it says "something e" instead of "something else". I assume this is because the ColumnWidths property is set too small.
Picture of my Combo box with ColumnWidths of 18 pt. The scroll bar is gone (which is what I want). 18 pt is the biggest value I came up without having a scroll bar. But it now displays only a few (18?) points in any row of the drop down menu which is unacceptable to me.
Any ideas how to solve this?
As Siddharth Rout pointed out, the problem was due to multiple columns. Setting the ColumnCount property back to 1 solves this issue.

Missing Properties in VBA [duplicate]

I'd like to add a horizontal scrollbar to a VBA ListBox.
It appears that the built in ListBox does not add a horizontal scrollbar automatically. I have a number of fields whose contents exceed the width of the ListBox and are thus unreadable to the user.
I found this article, however the code fails, due to accessing hwnd of the ListBox (which is apparently not available in VBA). I'd rather not write a native DLL to accomplish this as I suspect there is a better way.
Any idea on how I can add a horizontal scrollbar to a VBA ListBox?
I'm open to the idea of using an alternate control rather than getting it to work with the ListBox specifically.
Did you try ColumnWidths property?
I have listbox with horizontal scroll bar. I just had to add ColumnWidths property.
For example I have
me.Listbox1.Columnwidts ="0.5 in;0.2 in;1.5 in;0.75 in;0.5 in"
Unless I'm missing something, a VBA listbox will automatically gain a horizontal scrollbar if the total of its ColumnWidths property exceeds its own width.
There are no properties I know of that affect this behaviour, i.e. I don't otherwise know how to force or disable display of the horizontal scrollbar.
Access will automatically add a horizontal scrollbar if the column width exceed the width of the listbox. HOWEVER, if you are using multiple columns, the first column cannot be set to 0. You must have at least some value in there, even if it's just 0.1" Hope this helps.
In that article, the only reason it's getting ScaleMode is to set the width of the horizontal scroll bar. You don't have to do that.
SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, 800, 0
where 800 is the pixel width you want the list box to be able to scroll right to.
You will still need the hWnd. Best bet there is to use an external DLL (written in VB) which can enum through child windows of your process until it finds the windows class for the listbox (you will need to find some way to uniquely identify its parent, such as the window title/text or something). That same DLL could also do the SendMessage call above to set the horizontal text extent (perhaps also it could measure the width of the contained list items).
Handle to he list box can be obtained as follows :-
Dim ListHwnd As Integer
lstboxName.SetFocus
ListHwnd = GetFocus()
Use this ListHwnd as the first parameter to the sendmessage function...
We need to provide the declaration below,Since GetFocus function is not present in VBA by default
Private Declare Function GetFocus Lib "user32" () As Integer
In Visual Studio 2017, you can click on the list box, then go to the properties panel, and then (scroll down to) find the 'HorizontailScrollbar' property. By default this is property is set to false, so you should set it to true.
You know you have set the scroll bar properly when a small triangle appears in the top right corner of the list box.
Hope this helps.

Excel: Is there a way to programmatically change the background colours of individual cells in a multi column listbox control

I have a multi-column list box control embedded in Excel, is there a way to change the background, font, and other attributes of an individual cell (rather than changing all the text in the listbox)?
I don't mind which listbox control I use (activeX or otherwise).
I would also consider using another control that isn't tied into the excel worksheet rows and columns (i.e. I can't use a table).
Excel's built in listbox control doesn't really have formatting options, but you can do quite a bit with the listview control.
You'll have to add a reference to Microsoft Windows Common Controls 6.0 (from VBA window go to Tools and click References, and scroll to find it). There's not an effective way to change the background color of a single cell in the listview table, but you can change the font color and make text bold in specific cells or based on criteria.
The syntax to populate the listview is a bit different than listbox, but there's quite a bit of material online for this if you think its the way to go. Also - I have only used listview in a form, but it should be possible to add this as an ActiveX control in an actual sheet.

Combobox not displaying the data items

I have been trying to add Data items for my combobox in the DialogWindow of my mfc project, at design time by setting strings delimited by semi-colons for the Data property of this combobox.
But it just shows empty combobox at runtime. I tried using AddString() method of this combobox manually in the OnInitDialog() event of the DialogBox too. But that was also not working.
Tried creating a demo project again and when I added the combobox and put the value a;b in the Data property of it, it is showing up. But tried the same on my project and it is still showing the empty list in the new combobox! Tried to compare both project's code, like the header files and initialization's etc. Found that both are same. Other than some event declaration for the buttons and my custom c++ code in my project, the rest of the code is same as that of the newly created demo project!
Why in my project this combobox is showing an empty list of items? :(
EDIT:
Just noticed that the items are already in the list. But the dropdown is of small height and thus preventing the actual items from showing up! :(
Tried to look increasing the size of the combobox, but I couldn't find any property in the Property Window and dragging the corners of th combobox doesn't increases the height either! :(
Increase the size of the dropdown list like this, in the design window:
Click on the arrow on the right side of the combobox (highlighted in yellow on first pic). Then click and drag the bottom mid resize marker to increase the height (highlighted in the second pic).
This will increase the size of the dropdown list.
Otherwise when we try to click on the resize markers to increase the height, it would be disabled. So make sure you click on the arrow on the right side of the combobox first.

Why is Autofit Labeling greyed out?

I Have created a bar chart in a 2007 .xls file, but the data labels cannot be resized. When you go to the "format Data Labels" and then click on alignment, the Autofit and Internal Margin option is grayed out. Anyone know why?
Short version: It is greyed out for chart text, because it the option doesn’t apply.
Long version:
This is because on a chart, chart text do need to use margins, rather you can you can just move the text., and set its X and Y position. For example take the Chart Title, if you want more space to the left, just click and drag the title to the right. If you want the chart title to be bigger you just set a larger font, you have complete control over the title, as well as all other text on that chart, axis titles, legends, data labels, etc.
Internal margins are meant to be used when you don’t have complete control take for example text in a shape. If you insert a shape in your workbook, and add text to it, you cannot just pick the text up and move it to the right if you want more space on the left, so you can set the margin.
Here's a trick that worked for me:
My problem was: I added more text to the axis title and it was too long for it's original shape so some text went into second line.
My solution: I type the complete text somewhere else, copy the text, double click on the axis title and delete the original text -- the cursor would still be there -- now insert the copied text. Done! They are all in one line!
Partial solution to resize label to a single line: Data labels in a chart will often wrap themselves, when you dont want them to. They would often seem better in a vertical single line above the graph item. Reset them to one single line by editing the text in the label, deleting the Excel imposed return, and replace it with a space. The line, and label box, will resize to fit a single line. However because you have "manually interfered with the lable, it will no longer update for changing graph data. You would have to double click each lable then reset it. Most annoying.
A way around it is to make the chart area big. The data label boxes will resize to fit the words in one line. Then make the chart small (not the chart area).
Manually insert a textbox into the bar chart and type in the label that way. It's annoying and labor-intensive, but it works.

Resources