Solved

VISUAL BASIC EXCELL - DATE PROBLEM

Posted on 2013-02-01
9
441 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple Open Excel Spreadsheets 12 39
Excel lookup tables & values 7 24
Dropbox in Windows Server 2008 4 28
Excel - DATEDIF error #NUM 6 10
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
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.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

805 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