Date and Time Import Wrong Format

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have a userform that populates the textboxes by looking up the record based on the combobox value. There are a couple of textboxes that are dates and the formatting is wrong - 02/16/15 in the table imports into the textbox as 42086, which I understand. I tried setting the focus on the box after importing trying to fire the change event but that is now working. HELP me please - how can I present it correctly?

Also, the time in the spreadsheet is formatted as HH:MM which was added by another userform but when I import it the time is wrong - it is military time but 12:00 imports as 12:05 AM into the textbox, why? any ideas?

Here is my code for the DateIn textbox and also for my TimeIn textbox, your help is greatly appreciated.

Code:
'########## DATE IN
Private Sub txt_dateIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.txt_dateIn
        If Len(.Value) = 0 Then
            .SetFocus
        End If
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    If txt_dateIn = vbNullString Then
        Exit Sub
    If IsDate(txt_dateIn) Then
        txt_dateIn = Format(txt_dateIn, "Short Date")
    Else
        MsgBox "Non valid date"
        Cancel = True
    End If
    End If
    txt_dateIn.BackColor = 16777215
End Sub

Private Sub txt_dateIn_Change()
Dim Char As String
    Dim x As Date
    Dim y As Date
    Char = Right(txt_dateIn.Text, 1)
    Select Case Len(txt_dateIn.Text)
    Case 1 To 2, 4 To 5, 7 To 8
        If Char Like "#" Then
            If Len(txt_dateIn) = 8 Then
                On Error Resume Next
                x = DateValue(txt_dateIn.Text)
                y = DateSerial(Right(txt_dateIn, 2), Mid(txt_dateIn, 4, 2), Left(txt_dateIn, 2))
                If Err = 0 And x = y Then
                    On Error GoTo 0
                    Exit Sub
                Else
                    Err.Clear
                    On Error GoTo 0
                    txt_dateIn.SelStart = 0
                    txt_dateIn.SelLength = Len(txt_dateIn.Text)
                    'MsgBox "Please enter a valid date in the form dd/mm/yy", vbCritical + vbOKOnly, "Error"
                    Exit Sub
                End If
            Else
                Exit Sub
            End If
        End If
    Case 3, 6
        If Char Like "/" Then Exit Sub
    End Select
    Beep
    On Error Resume Next
    txt_dateIn.Text = Left(txt_dateIn.Text, Len(txt_dateIn.Text) - 1)
    txt_dateIn.SelStart = Len(txt_dateIn.Text)
End Sub
Private Sub txt_dateIn_Enter()
    txt_dateIn.BackColor = 13434879
End Sub


'########## TIME IN
Private Sub txt_timeIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    txt_timeIn.BackColor = 16777215
    If IsDate(Me.txt_timeIn.Value) Then
        Me.txt_timeIn.Value = Format(Me.txt_timeIn.Value, "HH:MM AM/PM")
    Else
        MsgBox "Input time as HH:MM military"
        Me.txt_timeIn.Value = ""
        Cancel = True
    End If
 End Sub

Private Sub txt_timeIn_Change()
    Me.txt_timeIn.Value = Format(Me.txt_timeIn.Value, "HH:MM AM/PM")
End Sub
Private Sub txt_timeIn_Enter()
    txt_timeIn.BackColor = 13434879
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Here you go -
Code:
'########## MACRO TO POPULATE FIELDS BASED ON Charge CHANGE
Private Sub cbo_charge_Change()
    If Me.cbo_charge.ListIndex > -1 Then
        Me.txt_dateIn.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 2, False)
        Me.txt_timeIn.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 3, False)
        Me.txt_dateOut.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 4, False)
        Me.txt_timeOut.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 5, False)
        Me.cbo_status.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 7, False)
        Me.cbo_location.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 8, False)
        Me.cbo_kiln.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 9, False)
        Me.cbo_dimension.Value = WorksheetFunction.VLookup(CLng(Me.cbo_charge.Value), _
            Sheets("data").Range("dataTable"), 11, False)
    End If
    Me.txt_dateIn.SetFocus
    Me.txt_timeIn.SetFocus
    Me.cbo_charge.SetFocus
End Sub
 
Upvote 0
Is cbo_charge populated from column 1 of the dataTable? If yes, try something like this...

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] cbo_charge_Change()
    
    [color=darkblue]Dim[/color] r [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=darkblue]If[/color] Me.cbo_charge.ListIndex > -1 [color=darkblue]Then[/color]
    
        r = Me.cbo_charge.ListIndex + 1 [color=green]'the relative row number of the combobox data[/color]
        
        [color=darkblue]With[/color] Sheets("data").Range("dataTable")
            Me.txt_dateIn.Value = [B]Format(.Cells(r, 2).Value, "Short Date")[/B]
            Me.txt_timeIn.Value = [B]Format(.Cells(r, 3).Value, "hh:mm")[/B]
            Me.txt_dateOut.Value = [B]Format(.Cells(r, 4).Value, "Short Date")[/B]
            Me.txt_timeOut.Value = [B]Format(.Cells(r, 5).Value, "hh:mm")[/B]
            Me.cbo_status.Value = .Cells(r, 7).Value
            Me.cbo_location.Value = .Cells(r, 8).Value
            Me.cbo_kiln.Value = .Cells(r, 9).Value
            Me.cbo_dimension.Value = .Cells(r, 11).Value
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]

You wouldn't require your other code to convert the formats.
 
Upvote 0
Yes, the cbo_charge info is in the first column of my table. I will give this a try.
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,341
Members
451,638
Latest member
MyFlower

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