Solved

VISUAL BASIC EXCELL - DATE PROBLEM

Posted on 2013-02-01
9
447 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
converting excel into labels 6 34
Finding Certain Criteria 4 35
Recolor all specifically named pie charts using VBA 4 35
NEED TO LOOK FOR EXACT NUMBER 14 40
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

736 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