Listbox format data in list

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

i use the following code to get data in my listbox;

VBA Code:
Private Sub UserForm_Initialize()
Dim lindex&
'Puts the user form in the middle of the Excel screen when opened.
    Me.StartupPosition = 0
    Me.Top = (Application.Height / 2) - (Me.Height / 2)
    Me.Left = (Application.Width / 2) - (Me.Width / 2)
Me.lstSearchResults.ColumnCount = 4
Me.lstSearchResults.List = Sheets("BMRcloset").[F7:I37].Value
With Me.lstSearchResults
.List(lindex, 0) = (Format((.List(lindex, 0)), "dd/mmm/yy"))
End With
End Sub

Now only the first data in the listbox is formatted right.
How can i make it that all the data is formatted right?

2023-03-10 200932.png
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
try adding
Dim i As Integer
Then after Me.Left line

VBA Code:
With Me.lstSearchResults
    .ColumnCount = 4
    .lstSearchResults.List = Sheets("BMRcloset").[F7:I37].Value
    For i = 0 To .ListCount - 1
        .List(lindex, i) = (Format((.List(lindex, i)), "dd/mmm/yy"))
    Next
End With
 
Upvote 0
Thanks for your reply.

I made this from it;

VBA Code:
Private Sub UserForm_Initialize()
Dim lindex&
Dim i As Integer
'Puts the user form in the middle of the Excel screen when opened.
    Me.StartupPosition = 0
    Me.Top = (Application.Height / 2) - (Me.Height / 2)
    Me.Left = (Application.Width / 2) - (Me.Width / 2)

With Me.lstSearchResults
    .ColumnCount = 4
    .lstSearchResults.List = Sheets("BMRcloset").[F7:I37].Value
    For i = 0 To .ListCount - 1
        .List(lindex, i) = (Format((.List(lindex, i)), "dd/mmm/yy"))
    Next
End With
End Sub

It gives the error; compile error: Method or data member not found.
It highlights .lstSearchResults in the with me part.
 
Upvote 0
Hi,
assuming that your data is formatted as required in the ranges then see if this update to your code will do what you want

VBA Code:
Private Sub UserForm_Initialize()
    Dim r             As Long, c As Long
    Dim arr()       As Variant
    Dim rng         As Range
   
    Set rng = ThisWorkbook.Worksheets("BMRcloset").Range("F7:I37")
   
    ReDim arr(1 To rng.Rows.Count, 1 To rng.Columns.Count)
   
    For r = 1 To UBound(arr, xlRows)
        For c = 1 To UBound(arr, xlColumns)
            arr(r, c) = rng.Cells(r, c).Text
        Next c
    Next r
   
    'Puts the user form in the middle of the Excel screen when opened.
    Me.StartupPosition = 0
    Me.Top = (Application.Height / 2) - (Me.Height / 2)
    Me.Left = (Application.Width / 2) - (Me.Width / 2)
   
    With Me.lstSearchResults
        .ColumnCount = UBound(arr, xlColumns)
        .List = arr
    End With
   
End Sub

Dave
 
Upvote 0
Solution
That works perfectly!

Now i have this problem, maybe you know.

Does this code fit in;

VBA Code:
Private Sub UserForm_Initialize()
Dim Ary as variant
'Puts the user form in the middle of the Excel screen when opened.
    Me.StartupPosition = 0
    Me.Top = (Application.Height / 2) - (Me.Height / 2)
    Me.Left = (Application.Width / 2) - (Me.Width / 2)

   Ary = Sheets("BMR data").Range("A3", Sheets("BMR data").Range("A" & Rows.Count).End(xlUp).Offset(, 22)).Value
   With Me.lstSearchResults
      .ColumnCount = 22
      .ColumnWidths = "0;100;0;0;0;0;0;0;0;100;0;0;100;0;0;100;0;0;100;0;0;1"
      .List = Ary
   End With

With this code i can exclude columns from my listbox.

Is it possible to use your solution in above code?
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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