Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VISUAL BASIC EXCELL - DATE PROBLEM

Posted on 2013-02-01
9
Medium Priority
?
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

704 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