format number(#,##0.00) & date(yyyy/mm/dd) doesn't work in listbox on userfrom

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,430
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I have this part of my project
Rich (BB code):
Private Sub LBoxPop()
  Data = ws.Cells(1).CurrentRegion.Value: x = 0
  ReDim Temp(1 To UBound(Data, 1), 1 To 5)
  For i = 1 To UBound(Data)
      x = x + 1
      Temp(x, 1) = Format(Data(x, 1), "yyyy/mm/dd")
       Temp(x, 3) = Format(Data(x, 3), "#,##0.00")
       Temp(x, 4) = Format(Data(x, 4), "#,##0.00")
       Temp(x, 5) = Format(Data(x, 5), "#,##0.00")
      For ii = 1 To 5
        Temp(x, ii) = Data(i, ii)
      Next ii
    'End If
  Next i
  With UserForm1.ListBox1
    .ColumnCount = 5
    .columnWidths = "80;240;120;120;120"
    .List = Temp
  End With
End Sub
I try showing format date in first column in listbox like this 2023/08/06
and formatnumber in column 3,4,5 like this 200,220.00
but doesn't work for me as in code above and bold lines
any help to fix it ,please ?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,
assuming that the ranges in your worksheet are displaying the required formats then see if this update to your code does what you want

VBA Code:
Private Sub LBoxPop()
    Dim r          As Long, c As Long
    Dim Data()     As Variant
    Dim rng        As Range
  
    Set rng = ws.Cells(1, 1).CurrentRegion
    ReDim Data(1 To rng.Rows.Count, 1 To rng.Columns.Count)
   
    For r = 1 To UBound(Data, xlRows)
        For c = 1 To UBound(Data, xlColumns)
            Data(r, c) = rng.Cells(r, c).Text
        Next c
    Next r
   
    With UserForm1.ListBox1
        .ColumnCount = 5
        .columnWidths = "80;240;120;120;120"
        .List = Data
    End With
   
End Sub

I am also assuming that your object variable ws has been intialized elsewhere in your project.

Dave
 
Upvote 0
Solution
Hello
I have this part of my project
Rich (BB code):
Private Sub LBoxPop()
  Data = ws.Cells(1).CurrentRegion.Value: x = 0
  ReDim Temp(1 To UBound(Data, 1), 1 To 5)
  For i = 1 To UBound(Data)
      x = x + 1
      Temp(x, 1) = Format(Data(x, 1), "yyyy/mm/dd")
       Temp(x, 3) = Format(Data(x, 3), "#,##0.00")
       Temp(x, 4) = Format(Data(x, 4), "#,##0.00")
       Temp(x, 5) = Format(Data(x, 5), "#,##0.00")
      For ii = 1 To 5
        Temp(x, ii) = Data(i, ii)
      Next ii
    'End If
  Next i
  With UserForm1.ListBox1
    .ColumnCount = 5
    .columnWidths = "80;240;120;120;120"
    .List = Temp
  End With
End Sub
I try showing format date in first column in listbox like this 2023/08/06
and formatnumber in column 3,4,5 like this 200,220.00
but doesn't work for me as in code above and bold lines
any help to fix it ,please ?
Try using the .AddItem method. It worked for me recently.
 
Upvote 0
I am also assuming that your object variable ws has been intialized elsewhere in your project.
yes that's correct
your new version works perfectly .(y)
thank you so much , Dave .:)
 
Upvote 0

Forum statistics

Threads
1,215,681
Messages
6,126,191
Members
449,298
Latest member
Jest

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