How to format Listbox Columns as Currency in Excel

Kheff

New Member
Joined
Jul 24, 2022
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi I've just started using a ListBox in Excel to view a table from one sheet on another.
I've been able to do this using very simple code as

VBA Code:
Private Sub UserForm_Initialize()
  Me.ListBox1.List = Worksheets("Cashflows").Range("A4:H26").Value
End Sub

However, as this does not include formatting, the original data which may look like this:
excelsheet.PNG


is displayed as:
userform.PNG


I'm wondering if it's possible to format the data displayed on the listbox as it is in the original sheet (column A as an integer and columns B to E as currency separated by commas with no decimals.
How would I go about this ?

Thank you so much
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi,
welcome to forum

To return what you see in a cell rather than its underlying value, you used the Range.Text property.

This does though, mean that you cannot use that simple line of code approach as you need to step through each cell in specified range.

Try this code & see if returns result you want

Code:
Private Sub UserForm_Initialize()
    Dim r           As Long, c As Long
    Dim ColWidths   As String
    Dim rng         As Range
    Dim arr()       As Variant
   
    Set rng = ThisWorkbook.Worksheets("Cashflows").Range("A4:H26")

    ReDim arr(1 To rng.Rows.Count, 1 To rng.Columns.Count)
   
    For r = 1 To rng.Rows.Count
        For c = 1 To rng.Columns.Count
            arr(r, c) = rng.Cells(r, c).Text
        If r = 1 Then ColWidths = ColWidths & rng.Cells(r, c).ColumnWidth * 5.6 & ";"
        Next c
    Next r
   
    With Me.ListBox1
        .RowSource = ""
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count
        .ColumnWidths = ColWidths
        .List = arr
    End With
   
End Sub

As you are using Accounting Format in you range, likely that the column widths in your listbox will need to be adjusted to accommodate the currency spacing so I have included some code that hopefully, will do this for you.

Dave
 
Upvote 0
Hi,
welcome to forum

To return what you see in a cell rather than its underlying value, you used the Range.Text property.

This does though, mean that you cannot use that simple line of code approach as you need to step through each cell in specified range.

Try this code & see if returns result you want

Code:
Private Sub UserForm_Initialize()
    Dim r           As Long, c As Long
    Dim ColWidths   As String
    Dim rng         As Range
    Dim arr()       As Variant
  
    Set rng = ThisWorkbook.Worksheets("Cashflows").Range("A4:H26")

    ReDim arr(1 To rng.Rows.Count, 1 To rng.Columns.Count)
  
    For r = 1 To rng.Rows.Count
        For c = 1 To rng.Columns.Count
            arr(r, c) = rng.Cells(r, c).Text
        If r = 1 Then ColWidths = ColWidths & rng.Cells(r, c).ColumnWidth * 5.6 & ";"
        Next c
    Next r
  
    With Me.ListBox1
        .RowSource = ""
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count
        .ColumnWidths = ColWidths
        .List = arr
    End With
  
End Sub

As you are using Accounting Format in you range, likely that the column widths in your listbox will need to be adjusted to accommodate the currency spacing so I have included some code that hopefully, will do this for you.

Dave
Hey Dave,
Thank you so much for your reply, it works fantastically !
Just wondering, is it possible to be able to carry strings across as well?
Appreciate the help :)
 
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