Userform Listbox reversing date format from database.

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi,
Can someone tell me why my listbox suddenly started flipping the date format form dd/mm/yyyy to mm/dd/yyyy. Obviously I must have changed something but I cannot figure out what
The database which is the source shows the correct format.

This is the listbox related part of my reset procedure for the userform
VBA Code:
        .lblMths.Caption = "Upcoming Transactions."
        .lstTrans.Visible = True
        .lstTrans.Width = 470
        .lstTrans.Height = 200
        .lstTrans.ColumnCount = 7
        .lstTrans.ColumnHeads = False
        .lstTrans.TextAlign = fmTextAlignLeft
        .lstTrans.ColumnWidths = "70,130,120,70,70,0,0"
        If DbLastRow > 1 Then
            .lstTrans.List = Range("Database").Value
        Else
            .lstTrans.List = DbWs.Range("A2:G2").Value
        End If
This is the code behind the listbox. This selects the row clicked on a shows it in a single line listbox. This too now shows the date incorrectly.
VBA Code:
Private Sub lstTrans_Click()
Dim i As Long

    Me.txtRowNo.Value = Val(Me.lstTrans.ListIndex + 1)
    i = Me.txtRowNo.Value + 1
    Me.lstTrans.ColumnHeads = False
    If Me.lstTrans.ListIndex <> -1 Then
        Me.lstSelected.Height = 18
        Me.lstSelected.Font.Bold = True
        Me.lstSelected.List = DbWs.Range("A" & i, "G" & i).Value
        Me.lstSelected.ColumnCount = 7
        Me.lstSelected.ColumnHeads = False
        Me.lstSelected.ColumnWidths = "72,122,122,70,70,0,0"
    End If

The database is simple and userform, a screenshot below
Any ideas?
 

Attachments

  • Userform.jpg
    Userform.jpg
    66.1 KB · Views: 11
  • Untitled.png
    Untitled.png
    18.5 KB · Views: 10
You could consider trying to read each cell in your range into a variant array as Text - This should preserve how the cell is formatted & hopefully, display correctly in your listbox

VBA Code:
Me.lblMths.Caption = "Upcoming Transactions."
    With Me.lstTrans
      .Visible = True
      .Width = 470
      .Height = 200
      .ColumnCount = 7
      .ColumnHeads = False
      .TextAlign = fmTextAlignLeft
      .ColumnWidths = "70;130;120;70;70;0;0"
  
      .List = GetList(Range("DataBase"))
    
    End With

Place following in a STANDARD module

VBA Code:
Function GetList(ByVal Target As Range) As Variant
    Dim arr()       As Variant
    Dim r           As Long, c As Long
  
    ReDim arr(1 To Target.Rows.Count, 1 To Target.Columns.Count)
  
    For r = 1 To UBound(arr, 1)
        For c = 1 To UBound(arr, 2)
            arr(r, c) = Target.Cells(r, c).Text
        Next c
    Next r
    GetList = arr
End Function

Code not tested but if suggestion does what you want you can use function in your project for other listboxes if need to overcome similar issues.

Hope Helpful

Dave
I will definitely look into this, I think it will be a huge improvement from what I currently have.
Thanks for the reply.
Dannielle
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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