Listbox date formate problem

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to load a Listbox on a userform;

Code:
With Sheets("Referrals")
lbtarget.List = .Range("A3", .Range("L65000").End(xlUp)).Value
End With

Columns 3 and 4 of the sheet are date values, but when they appear in the Listbox they have been converted to US format - can this be stopped or worked around?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You may need to do it the long way, similar to below:-
Code:
Dim Rng As Range
Dim Ac As Range
Dim Dn As Range
Set Rng = Range("A1:H12")
ListBox1.ColumnCount = 8
ListBox1.Clear
ListBox1.ColumnWidths = "50,50,80,80,50,50,50,50"
        For Each Dn In Rng.Rows
             For Each Ac In Dn.Columns
                With ListBox1
                   If Ac.Column = 1 Then
                        .AddItem Ac
                    Else
                    If Ac.Column = 3 Or Ac.Column = 4 Then
                        .List(.ListCount - 1, Ac.Column - 1) = Format(Ac, "dd/mm/yyyy")
                       Else
                        .List(.ListCount - 1, Ac.Column - 1) = Ac
                    End If
                    End If
                End With
            Next Ac
        Next Dn
 
Upvote 0
MickG, thanks - I need the range though to be dynamic, in that it only grabs the used range. I've tried this and I get 'Object Required' with the 'Set Rng' line failing;

Code:
Dim Rng As Range
Dim Ac As Range
Dim Dn As Range
Set Rng = Sheets("Referrals").Range("A3", Range("L65000").End(xlUp)).Value 'Range("A1:H12")
ListBox1.ColumnCount = 12
ListBox1.Clear
ListBox1.ColumnWidths = "70;70;70;100;100;100;100;100;100;100;100;100"
        For Each Dn In Rng.Rows
             For Each Ac In Dn.Columns
                With ListBox1
                   If Ac.Column = 1 Then
                        .AddItem Ac
                    Else
                    If Ac.Column = 3 Or Ac.Column = 4 Then
                        .List(.ListCount - 1, Ac.Column - 1) = Format(Ac, "dd/mm/yyyy")
                       Else
                        .List(.ListCount - 1, Ac.Column - 1) = Ac
                    End If
                    End If
                End With
            Next Ac
        Next Dn

Any ideas?
 
Upvote 0
Hi,
not tested but perhaps pass your cell values as text to a string array.

See if this helps:

Code:
Private Sub UserForm_Initialize()
    Dim rng As Range


    With Sheets("Referrals")
        Set rng = .Range(.Range("A3"), .Range("L" & .Rows.Count).End(xlUp))
        lbtarget.ColumnCount = rng.Columns.Count
        lbtarget.ColumnWidths = "70;70;70;100;100;100;100;100;100;100;100;100"
        lbtarget.List = StringArray(Target:=rng)
    End With


End Sub




Function StringArray(ByVal Target As Range) As String()
    Dim RangeArray() As String
    Dim c As Range
    
    ReDim RangeArray(1 To Target.Rows.Count, 1 To Target.Columns.Count) As String

    For Each c In Target
        RangeArray(c.Row - Target.Row + 1, c.Column) = c.Text
    Next c

    StringArray = RangeArray

End Function

Dave
 
Last edited:
Upvote 0
Try this in your Userform.
In the other method there appears to be a limit of the Number of column allowed.
NB:- I don't think you can call the Userform from another sheet , because it uses the range Address in th "RowSource"
Code:
Private Sub UserForm_Initialize()
   With Me.ListBox1
    .ColumnCount = 12
    .ColumnWidths = "70,70,70,100,100,100,100,100,100,100,100,100"
    .RowSource = Range(Range("A3"), Range("L" & Rows.Count).End(xlUp)).Address
   End With
End Sub
 
Last edited:
Upvote 0
Gents - thanks so much to both for taking the time to respond, the solution from MickG works.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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