PC Review


Reply
Thread Tools Rate Thread

Convert string to date problem

 
 
ian
Guest
Posts: n/a
 
      7th Nov 2007
I have strings in the format dd.mm.yyyy which I want to convert to
dates. ie 07.11.2007 is 7th Nov 2007.I'm in the UK, with UK regional
settings.


I tried Cdate which didn't work. I then used find and replace to
replace the " ." with a "/" as follows


Range("c" & i).Replace What:=".", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False,
_
ReplaceFormat:=False

The results are strange.

15.05.2007 changes to 15/05/2007 and if I check with ISNUMBER=False
( I can change to date with Cdate)

04.08.2007 changes to 08/04/2007 (8th of April when date was 4th Aug)
and if I check with ISNUMBER=True.

It seems when I do the find and replace with a "/" dates which are
legimitate as mm/dd/yyyy are conveted to a date number and other dates
just looking like dd/mm/yyyy are not converted to a date number.

Can anyone give me some pointers how to easilt change a string
dd.mm.yyyy to a date. I'm struggling with this.

Thanks

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2hhcmxlcyBDaGlja2VyaW5n?=
Guest
Posts: n/a
 
      8th Nov 2007
Ian, Try this:
Sub ConverttoDate()
Dim s1 As String
Dim d1 As Date
Dim sAr() As String
s1 = "7.11.2007"
sAr = Split(s1, ".")
d1 = DateSerial(sAr(2), sAr(1), sAr(0))
Debug.Print d1
End Sub

--
Charles Chickering

"A good example is twice the value of good advice."


"ian" wrote:

> I have strings in the format dd.mm.yyyy which I want to convert to
> dates. ie 07.11.2007 is 7th Nov 2007.I'm in the UK, with UK regional
> settings.
>
>
> I tried Cdate which didn't work. I then used find and replace to
> replace the " ." with a "/" as follows
>
>
> Range("c" & i).Replace What:=".", Replacement:="/", LookAt:=xlPart, _
> SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False,
> _
> ReplaceFormat:=False
>
> The results are strange.
>
> 15.05.2007 changes to 15/05/2007 and if I check with ISNUMBER=False
> ( I can change to date with Cdate)
>
> 04.08.2007 changes to 08/04/2007 (8th of April when date was 4th Aug)
> and if I check with ISNUMBER=True.
>
> It seems when I do the find and replace with a "/" dates which are
> legimitate as mm/dd/yyyy are conveted to a date number and other dates
> just looking like dd/mm/yyyy are not converted to a date number.
>
> Can anyone give me some pointers how to easilt change a string
> dd.mm.yyyy to a date. I'm struggling with this.
>
> Thanks
>
>

 
Reply With Quote
 
ian
Guest
Posts: n/a
 
      8th Nov 2007
Charles

Great, simple and it works perfeclty.

Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA convert day and date from text string to Excel date Max Bialystock Microsoft Excel Programming 5 14th May 2007 04:54 AM
How to convert a date string to datetime value with custom date format? ABC Microsoft C# .NET 1 29th Sep 2005 11:11 AM
How to convert user-defined custom format date string to date value abcabcabc Microsoft VB .NET 1 19th Aug 2005 12:31 PM
Convert a string containing a 32-bit binary date to a date data type Remi Caron Microsoft C# .NET 2 22nd Sep 2004 06:23 PM
String Convert to Date Time Problem is Windows 98 Ryanfai Microsoft VB .NET 2 23rd Sep 2003 04:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:16 AM.