VBA date problem.

erivien

New Member
Joined
Jan 16, 2004
Messages
21
I have a problem with a VBA form.

the code in question gets a date from a cell in the worksheet and displays it in a text box input. (theadate)
it displays dd/mm/yyyy

The user has the option to change it via a tickbox (thedate)
then they change it and then when they press the command button the code saves the new date in the cell.
it saves mm/dd/yyyy

its confusing the dates because of the inane americans!

the code looks like:
Code:
Private Sub UserForm_Initialize()
    Dim shtActive As Worksheet
    Set shtActive = Application.Workbooks("system.xls").Worksheets("stats")
    theadate = shtActive.Range("d2").Value
End Sub

Private Sub complete_Click()
    Dim shtActive As Worksheet
    Set shtActive = Application.Workbooks("system.xls").Worksheets("stats")
    If thedate = False Then
        shtActive.Range("d2").Value = theadate.Value
    Else
        shtActive.Range("d2").Formula = "=TODAY()"
    End If
    Unload fsettings
End Sub

thanks for any help

regards
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

At an appropriate point, try

shtActive.Range("d2").Numberformat = "ddmmyyyy"

HTH

alan
 
Upvote 0
ye im sorri bout that :P... though i do stand by the inane comment. You do spell colour and center better but the american date is ilogical!

As the cell in question is already formatted in the dd/mm/yyyy format. It is the value that the vba is writing to it, eg (34650) that is the problem because date formatting is just displaying dd/mm/yyyy from the value...

I'll just make 3 fields makes it simpler :)
thanks for your help though...
 
Upvote 0
This worked for me:

Code:
Range("A1").Value = DateValue(theadate.Text)

The VBA DateValue function recognizes the order for month, day, and year according to the Short Date format you specified for your system.
 
Upvote 1
i now have one small problem... When the form does the initialize it displays the mm/dd/yyyy in the text box although the cell is dd/mm/yyyy (i perhaps did not notice this before as its the 2nd of febuary!

I would also like to put some kind of mask on the field so a user can only enter:
dd/mm/yyyy
if they enter
333 it will not accept the third 3. a user must use a / as the 3rd and 6th value while the others must be numbers. at the moment i am using this:
Code:
Private Sub theadate_Change()
    Dim Char As String
    Char = UCase(Right(theadate.Text, 1))
    If Char Like "#" Then
        Exit Sub
    ElseIf Char Like "/" Then
        Exit Sub
    Else
        Beep
        On Error Resume Next
        theadate.Text = Left(theadate.Text, Len(theadate.Text) - 1)
        theadate.SelStart = Len(theadate.Text)
    End If
End Sub
and the field has a max length of 10, however i need to really set the correct constraints on the slashes.

These are such little things that no doubt the end user will take for granted but i am somewhat of a perfectionist lol...
 
Upvote 0
bump ^ i know bumping is annoying but i have another problem that was a product of this solution, kinda. I have sorted the first of the probelms above with a format() but...

The masking validation is still a problem.

any help is appreciated :)
 
Upvote 0
This should give the right date in the TextBox:

Code:
theadate = Format(shtActive.Range("d2").Value,"dd/mm/yy")

And here is a mask:

Code:
Private Sub TextBox1_Change()
    Dim Char As String
    Dim x As Date
    Dim y As Date
    Char = Right(TextBox1.Text, 1)
    Select Case Len(TextBox1.Text)
    Case 1 To 2, 4 To 5, 7 To 8
        If Char Like "#" Then
            If Len(TextBox1) = 8 Then
                On Error Resume Next
                x = DateValue(TextBox1.Text)
                y = DateSerial(Right(TextBox1, 2), Mid(TextBox1, 4, 2), Left(TextBox1, 2))
                If Err = 0 And x = y Then
                    On Error GoTo 0
                    Exit Sub
                Else
                    Err.Clear
                    On Error GoTo 0
                    TextBox1.SelStart = 0
                    TextBox1.SelLength = Len(TextBox1.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
    TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
    TextBox1.SelStart = Len(TextBox1.Text)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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