Solved

VISUAL BASIC EXCELL - DATE PROBLEM

Posted on 2013-02-01
9
437 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
9 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 166 total points
ID: 38846295
Change to

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

Accepted Solution

by:
broro183 earned 168 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 166 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now