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
 
woah thats alot of vba

I jus figured a way to do it, alot shorter too!

Code:
        If theadate.Text Like "##/##/####" Then
            shtActive.Range("d2").Value = DateValue(theadate.Text)
        Else
            MsgBox prompt:="Please enter a valid date format.", _
            Buttons:=vbOKOnly + vbCritical + vbDefaultButton1, _
            Title:="Alternative Date unrecognised"
        End If
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
erivien said:
woah thats alot of vba

I jus figured a way to do it, alot shorter too!

Code:
        If theadate.Text Like "##/##/####" Then
            shtActive.Range("d2").Value = DateValue(theadate.Text)
        Else
            MsgBox prompt:="Please enter a valid date format.", _
            Buttons:=vbOKOnly + vbCritical + vbDefaultButton1, _
            Title:="Alternative Date unrecognised"
        End If

Yes, but what if the user enters 31/02/2004. You will get 2nd March.
 
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...

Actually the English spellings are the correct ones and make much more sense. You seem to prefer a mixture as you have spelt colour the English (correct) way and center the American (incorrect) way.
 
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.
Man you are a life saver. I'm short of word.
 
Upvote 0

Forum statistics

Threads
1,216,091
Messages
6,128,772
Members
449,468
Latest member
AGreen17

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