Wikiposts
Search
Computer/Internet Issues & Troubleshooting Anyone with questions about the terribly complex world of computers or the internet should try here. NOT FOR REPORTING ISSUES WITH PPRuNe FORUMS! Please use the subforum "PPRuNe Problems or Queries."

Excel/VBA date format query

Thread Tools
 
Search this Thread
 
Old 27th Aug 2014, 11:25
  #1 (permalink)  
Per Ardua ad Astraeus
Thread Starter
 
Join Date: Mar 2000
Location: UK
Posts: 18,579
Likes: 0
Received 0 Likes on 0 Posts
Excel/VBA date format query

VBA module copies a date (dd/mm/yyyy) from a TextBox on sheet A to new Sheet B column B. All well until day >12 when it pastes the date as text only.

Code does
Code:
Cells(z, 2) = Format(TextBox2.Text, "dd/mm/yyyy")
PC is set for UK data 'short' format
Column B is set for date format dd/mm/yyyy

Cannot seem to crack this one!
BOAC is offline  
Old 27th Aug 2014, 12:19
  #2 (permalink)  
 
Join Date: Mar 2003
Location: Aus
Posts: 192
Received 0 Likes on 0 Posts
Not very elegant but try this

Cells(z, 2)=DATEVALUE(LEFT(TextBox2.Text,2)&"/"&MID(TextBox2.Text,4,2)&"/"&MID(TextBox2.Text,7,4))

so long as the text box input is dd/mm/yyyy it should work.

You can then format the cell to any date format using the standard formatting method.

you can also use ISDATE to validate the textbox input as a valid date.

DATEVALUE works for any text that can be interpreted as a date. The problem is it won't consistently interpret data such as 6/5/2000 as 6 May 2000 ..sometimes it makes it 5 Jun. I found it difficult to make the date settings "stick". Writing code that doesn't rely on the international settings is better and saves the angst for people who don't change the default MS settings ...(or DO ..but don't know what they did!)

Last edited by Agent86; 27th Aug 2014 at 13:10.
Agent86 is offline  
Old 27th Aug 2014, 15:12
  #3 (permalink)  
Per Ardua ad Astraeus
Thread Starter
 
Join Date: Mar 2000
Location: UK
Posts: 18,579
Likes: 0
Received 0 Likes on 0 Posts
Thank you, Agent - that has fixed it! As you say "Not very elegant" but, hey, who cares in a module? As to why we have to resort to such trickery..............?

It appears to pick up on dd/mm/yy as well.
BOAC is offline  

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Contact Us - Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service

Copyright © 2024 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.