Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default Convert number to date

Hi! I am trying to convert a string to a date. however i get Error nr6
"spill".

in cell A1 I have a date written like this "20081012"

Sub anders()
Dim a As Long
a = Range("a1").Value
b = CDate(a)
End Sub


I get the error when trying to convert to date, pls hepl

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Convert number to date

Maybe

a = Range("a1").Value
mydate = Left(a, 4) & "/" & Mid(a, 3, 2) & "/" & Right(a, 2)

Mike

"Arne Hegefors" wrote:

Hi! I am trying to convert a string to a date. however i get Error nr6
"spill".

in cell A1 I have a date written like this "20081012"

Sub anders()
Dim a As Long
a = Range("a1").Value
b = CDate(a)
End Sub


I get the error when trying to convert to date, pls hepl

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Convert number to date

in a cell

=DATE(LEFT(A1,4),MID(A1,5,2), MID(A1,7,2))

or VBA

Sub test()
Dim v
Dim dt As Date
ActiveCell = 20081012

v = ActiveCell.Value
dt = DateSerial(Left$(v, 4), Mid(v, 5, 2), Mid(v, 7, 2))
MsgBox dt

End Sub


Regards,
Peter T


"Arne Hegefors" wrote in message
...
Hi! I am trying to convert a string to a date. however i get Error nr6
"spill".

in cell A1 I have a date written like this "20081012"

Sub anders()
Dim a As Long
a = Range("a1").Value
b = CDate(a)
End Sub


I get the error when trying to convert to date, pls hepl



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Convert number to date

The value 20081012 is not a proper date format, so CDate cannot convert it.
Try this instead (no need for your 'a' variable, you can assign to 'b'
variable directly)...

Sub anders()
b = CDate(Format(Range("A1").Value, "0000-00-00"))
End Sub

--
Rick (MVP - Excel)


"Arne Hegefors" wrote in message
...
Hi! I am trying to convert a string to a date. however i get Error nr6
"spill".

in cell A1 I have a date written like this "20081012"

Sub anders()
Dim a As Long
a = Range("a1").Value
b = CDate(a)
End Sub


I get the error when trying to convert to date, pls hepl


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert number to date value saz2369 Excel Discussion (Misc queries) 8 October 13th 09 03:10 PM
Convert number to date Teri Excel Discussion (Misc queries) 7 May 18th 07 06:29 PM
how do you convert a number into a date JCM Excel Worksheet Functions 2 April 6th 07 02:26 AM
Convert Date to number? JethroUK© Excel Worksheet Functions 6 May 8th 06 10:15 PM
How do I convert Date serial number to date rdunne Excel Worksheet Functions 1 April 12th 05 03:04 PM


All times are GMT +1. The time now is 08:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"