Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VISUAL BASIC EXCELL - DATE PROBLEM

Posted on 2013-02-01
9
Medium Priority
?
451 Views
Last Modified: 2013-09-25
Hi

I have a problem in visual basic for excel.

Introducing a date in a textbox in dd/mm/yyyy format appears in the related cell as introduced in the userform.

However, resetting a date from a cell into the userform, transforms the date in mm/dd/yyyy

So far I solved this problem by splitting dates in three text boxes, one for the day, one for the month, and one for the year. This work around conflicts with the restriction on the maximum number of text boxes and drop down boxes one excel file can contain ( about 500 I think, perhaps there’s no restriction as such, but when I develop userforms with more than 500 boxes, I get an error )

Do you have a solution for this "date resetting" problem  ?

Kind regards

Bart

in attachement you'll find a simple example illustrating this issue
datum-check.xlsm
0
Comment
Question by:BARTHRBD
7 Comments
 
LVL 14

Expert Comment

by:Faustulus
ID: 38846287
To tell you the sad truth, your example fails to produce the problem you describe on my computer. The probable reason is that my regional settings describe the Short Date as dd.mm.yy, and that is what I get both, in the form and the worksheet. I don't get dd/mm/yy and don't see where that format is set, either.
If you are producing the form for use on your own PC only I suggest your check your Regional settings. Else, the problem is a lot more complicated.
Basically, Excel is local. If your home format is dd/mm/yy Excel will work with that format (Regional Settings). VBA, on the other hand, is strictly American. It speaks, writes, calculates and thinks mm dd yy. The bridge between the two is the DateSerial. Try CDbl(DateSerial(2013,2,1)) for February 1, 2013. Provided you get your days and months straight in the form you can feed that number into Excel and it will write the correct date, subject to the applied cell format. Similarly, if you read this number back into your form  as Format(Sheet("TEST").Cells(6,5).Value, "dd/mm/yy") you will get the correct date displayed in your form.
Bear in mind that the standard date formats available in Format/Cells refer to the Regional Settings and are different on your computer as compared to mine. You can set a Custom format which will ignore and over-ride the Regional Settings.
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 664 total points
ID: 38846295
Change to

Sheets("TEST").Cells(5, 6) = CVDate(UserForm1.TextBox1)
0
 
LVL 10

Accepted Solution

by:
broro183 earned 672 total points
ID: 38846452
Try the attached file. Note, I have changed the textbox so that it doesn't have a control source because I found that if it was linked the reset button doesn't work.

hth
Rob
datum-check-v2.xlsm
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:broro183
ID: 38846457
Ooopps, I should have refreshed my browser before posting. Oh well, at least I've been thinking along the same lines as others with my use of DateSerial & Format(...

I had forgotten about Cdate & it is only now that I've looked up the Help files that I have learnt about CVDate.

Rob
0
 

Author Comment

by:BARTHRBD
ID: 39404657
sorry i'm not much of an experienced "expert exchange" user so i don't know how to end this "abandonnend question". Anyway i didn't find an appropriate solution for my question, and i think it has to do with regional settings and vba being an american application. Thanks anyway
0
 
LVL 10

Expert Comment

by:broro183
ID: 39404882
hi Bart,

I believe that a solution has been given to you in the existing answers. Have you read Faustaulus's post, tried CVDate as Ssquibh suggested, or tried my file from my previous post?

If this is still an issue for you, it can be solved, provided you persevere & give us more details of what you have tried & why each thing you have tried doesn't work.

Rob
0
 
LVL 14

Assisted Solution

by:Faustulus
Faustulus earned 664 total points
ID: 39405138
I'm sorry you didn't solve your problem. However, you couldn't honestly say that you tried to cooperate with any of us here, or could you?
The following code should work. Paste it into your test form's code module. Make sure that the cell on the worksheet is formatted as Date and that you leave the ControlSource property of the TextBox blank.
Private Sub CommandButton1_Click()

    Dim Sp() As String
    
    Sp = Split(UserForm1.TextBox1.Text, "/")
    If UBound(Sp) < 2 Then
        MsgBox "Invalid date"
        Exit Sub
    End If
    Sheets("TEST").Cells(5, 6).Value = CDbl(DateSerial(Sp(2), Sp(1), Sp(0)))
End Sub

Private Sub CommandButton2_Click()

    Dim Dt As Date
    Dim strDate As String
    
    Dt = CDate(Sheets("TEST").Cells(5, 6).Value)
    strDate = Format(Day(Dt), "00") & "/" & _
              Format(Month(Dt), "00") & "/" & _
              Format(Year(Dt), "0000 ")
    UserForm1.TextBox1.Text = strDate
End Sub

Open in new window

As I explained in my earlier post, my PC settings don't allow me to replicate the fault you are experiencing. My code may therefore seems excessive. However, you will not find your days and months mixed up.
Please share your experience.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question