Formatting the columns in a userform combobox

SJ-NY

Board Regular
Joined
Feb 16, 2008
Messages
156
All:


I have just started to incorporate userforms into my procedures. I have been successful in using a two column combobox.


My question is formatting. The first column is text and the second is numerical. Is it possible to apply different formatting to each column of the combobox? If it is, examples of coding would be great.


I tried posting this earlier today but it does not appear it went through. If this is a duplicate post my apologies.


Thanks for you help


Steve
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The columns of a combobox or a listbox are formatted the same.

But there are work-arounds. How do you want the text column (first column?) to be formatted and how do you want the numeric column to be formatted?
 
Upvote 0
Mike
Thank you for you quick response. I will end up with three columns in this order

text
numeric - general, no decimals
percentage

thank you for you help

Steve
 
Upvote 0
One thing to remember is that all the entries in a combobox (or listbox) are strings.

If you want the numerals in column 2 to appear as percentages, you can use the Format function.
For this test routine, I have names in column A and numbers in column B of the worksheet. This routine puts those values in a two column ListBox.
Code:
Dim oneCell As Range

With ListBox1
    .ColumnCount = 2
    .TextColumn = 1
    .BoundColumn = 2
    For Each oneCell In Range("A1:A4")
        .AddItem oneCell.Value
        .List(.ListCount - 1, 1) = Format(oneCell.Offset(0, 1).Value, "0.00 %; -0.00 %")
    Next oneCell
End With

Note that the .BoundColumn = 2 line makes the .Value property of the Listbox equal to the string in the second column.

If you want to calculate with that, it has to be converted from the string "12.3 %" to the number .123. The VBA function Val errors when there is a terminal "%" in its argument so Evaluate is used.
Code:
Private Sub ListBox1_Click()
    UserForm1.Caption = Evaluate(ListBox1.Value)
End Sub
I hope this helped.
ListBoxes and ComboBoxes act almost identically, so this code will also work with a ComboBox.
 
Last edited:
Upvote 0
MIke:

thank you. I will give this a try and let you know how it works.

Again thank you for your help.

Steve
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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