VBA- Listbox - ColumnCount>1 - adjust Column width

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
505
I am using a Listbox with a ColumnCount of 5 - I have two questions

1) How to adjust column width using the graphical GUI - similar to what you do when you adjust a column width in Excel itself using the mouse and click and drag.

2) How can I sort the entries in the listbox - is there a property similar to ExploreBar - in other words I would like to click in the header and then it sorts ascending - if you click again it sorts decenting.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Rasm

For 1) I think you'd need to make the whole Userform resizeable - without using the Windows API, you can only do this thru code. One very cool way of doing this is by Andy Pope's code here:

http://www.andypope.info/vba/resizeform.htm

2) You will need to design a custom sorting routine (eg a bubblesort) and refresh the list dynamically when the user eg clicked a sort button on the Userform.
 
Upvote 0
If you can use a ListView control instead, it's a lot easier. 1 is built-in and 2 only takes a little code.
 
Upvote 0
Roray

ohhh - just checked the properties for Listview - that sounds like what I need - Thanks

Richard
Thanks for link - I will check it as well - But sounds like Listview may be the ticket for what I am doing - Thanks
 
Upvote 0
Roray

That Listview is just what I needed - hehehe - does it have an explore bar option for sorting or do I need to code that myself?

I notice a sort option - but looks like you need to set a key for what column to use. Guess you cannot have it all.

Great stuff - thanks
 
Upvote 0
Richard - That resize code for the userforms works great - this forum is fantatstic - thanks.
 
Upvote 0
The code for sorting is minimal - I will post it when I'm back in front of a computer.
 
Upvote 0
Rasm

You can add buttons for the sorting or try the ListView's ColumnClick event.
Code:
Private Sub lvAgreements_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    lvAgreements.SortKey = ColumnHeader.Index - 1
    lvAgreements.Sorted = True
End Sub[code]
You could probably use that to do something with the column header to indicate if it's the one being sorted by.
 
For example change the icon in the heading, that might be a bit more complicated and I think you'd need to add a Image List control for the icons.
 
PS You can use icons in the list as well.
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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