Listbox displaying more decimal places than the worksheet it is referencing

ijhoeq

Board Regular
Joined
Jun 20, 2018
Messages
61
Hello,
I am trying to create a listbox that displays a table from a worksheet ('Liquid Charge Control Sheet') using the code below. The worksheet displays only one decimal place (i.e. 100.5) but when I reference that cell in the listbox it displays "100.50000000000000". Can someone show me how to display only one decimal place in the listbox? Thanks!

Code:
Private Sub Table_CommandButton_Click()


    Dim ws As Worksheet
    Dim rng As Range
    Dim MyArray
    
    Set ws = Sheets("Liquid Charge Control Sheet")
    
    Set rng = ws.Range("F31:H" & ws.Range("F" & ws.Rows.Count).End(xlUp).Row)
    
    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count
        
        MyArray = rng
        .List = MyArray
        .ColumnWidths = "50;50"
        .TopIndex = 0
    End With


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
As you see, the underlying values are being picked up, not the displayed values. Simply round those numbers before assigning them to your listbox...

Code:
Private Sub CommandButton1_Click()

    Dim ws As Worksheet
    Dim MyArray As Variant
    Dim i As Long
    
    Set ws = Sheets("Liquid Charge Control Sheet")
    
    MyArray = ws.Range("F31:H" & ws.Range("F" & ws.Rows.Count).End(xlUp).Row).Value
    
    For i = LBound(MyArray, 1) To UBound(MyArray, 1)
        MyArray(i, 1) = Round(MyArray(i, 1), 1)
    Next i
    
    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = UBound(MyArray, 2)
        
        .List = MyArray
        .ColumnWidths = "50;50"
        .TopIndex = 0
    End With

End Sub

By the way, you can replace...

Code:
 MyArray = ws.Range("F31:H" & ws.Range("F" & ws.Rows.Count).End(xlUp).Row).Value

with

Code:
    With ws
        MyArray = .Range("F31:H" & .Cells(.Rows.Count, "F").End(xlUp).Row).Value
    End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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