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

I have a 6 character string of numbers in a cell which I would like to
treat as a date, whereas I can add 1 to it in VBA and it would display
as the following day, and then assign that value to a variable. For
instance, 033109 would turn into 040109 by adding 1 to it in a
procedure. Is this possible to do in a macro?

Thanks...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default String as date

Look at this:

Sub aaa()
DateString = Range("A1").Value
MyMonth = Left(DateString, 2)
MyDay = Mid(DateString, 3, 2)
MyYear = Right(DateString, 2)

MyDate = MyMonth & "/" & MyDay & "/" & MyYear
MyDate = DateAdd("d", 1, MyDate)
DateString = "'" & Left(MyDate, 2) & Mid(MyDate, 4, 2) & Right(MyDate,
2)
Range("A1") = DateString
End Sub

Regards,
Per

On 7 Apr., 03:13, Steve wrote:
I have a 6 character string of numbers in a cell which I would like to
treat as a date, whereas I can add 1 to it in VBA and it would display
as the following day, and then assign that value to a variable. For
instance, 033109 would turn into 040109 by adding 1 to it in a
procedure. Is this possible to do in a macro?

Thanks...


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default String as date

Thanks, but using 033109 in A1, this sub returned '4//209

---
Steve

On Apr 6, 10:32*pm, Per Jessen wrote:
Look at this:

Sub aaa()
DateString = Range("A1").Value
MyMonth = Left(DateString, 2)
MyDay = Mid(DateString, 3, 2)
MyYear = Right(DateString, 2)

MyDate = MyMonth & "/" & MyDay & "/" & MyYear
MyDate = DateAdd("d", 1, MyDate)
DateString = "'" & Left(MyDate, 2) & Mid(MyDate, 4, 2) & Right(MyDate,
2)
Range("A1") = DateString
End Sub

Regards,
Per

On 7 Apr., 03:13, Steve wrote:

I have a 6 character string of numbers in a cell which I would like to
treat as a date, whereas I can add 1 to it in VBA and it would display
as the following day, and then assign that value to a variable. For
instance, 033109 would turn into 040109 by adding 1 to it in a
procedure. Is this possible to do in a macro?


Thanks...


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,942
Default String as date

hi
after this line.....
MyDate = DateAdd("d", 1, MyDate)

add this line
MyDate= Format(MyDate, "mm,dd,yy")

the unexpected results (i think) is from a DateAdd format change.
the line i added will put the format back to the original so that the rest
of the code can put in back on the sheet the way it came off.

Regards
FSt1

"Steve" wrote:

Thanks, but using 033109 in A1, this sub returned '4//209

---
Steve

On Apr 6, 10:32 pm, Per Jessen wrote:
Look at this:

Sub aaa()
DateString = Range("A1").Value
MyMonth = Left(DateString, 2)
MyDay = Mid(DateString, 3, 2)
MyYear = Right(DateString, 2)

MyDate = MyMonth & "/" & MyDay & "/" & MyYear
MyDate = DateAdd("d", 1, MyDate)
DateString = "'" & Left(MyDate, 2) & Mid(MyDate, 4, 2) & Right(MyDate,
2)
Range("A1") = DateString
End Sub

Regards,
Per

On 7 Apr., 03:13, Steve wrote:

I have a 6 character string of numbers in a cell which I would like to
treat as a date, whereas I can add 1 to it in VBA and it would display
as the following day, and then assign that value to a variable. For
instance, 033109 would turn into 040109 by adding 1 to it in a
procedure. Is this possible to do in a macro?


Thanks...



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 806
Default String as date

Hello,

Here is a function which works if called via worksheet or from within
VBA:
Function addone(s As String) As String
'Adds one day to date sring "mmddyy".
addone = Format(DateSerial(Right(s, 2), _
Left(s, 2), Mid(s, 3, 2) + 1), _
"MMDDYY")
End Function

Sub test()
Debug.Print "Day after tomorrow: " & _
addone("040709") 'Works
Debug.Print "Day after 31-Dec-2029: " & _
addone("123129") 'Beware 010130 will be 1-Jan-1930
End Sub

Please note that 123129 will be interpreted by Excel as 31-Dec-2029
but 010130 as 1-Jan-1930!

You might want to use a 4 char year presentation MMDDYYYY. Then change
the program to:

Function addone(s As String) As String
'Adds one day to date sring "mmddyyyy".
addone = Format(DateSerial(Right(s, 4), _
Left(s, 2), Mid(s, 3, 2) + 1), _
"MMDDYYYY")
End Function

Regards,
Bernd
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 Date string to date format Joe M. Excel Discussion (Misc queries) 7 May 6th 10 02:46 PM
Search and Collect Date from Cell Containing a String with a Date MJ Excel Programming 2 January 29th 09 11:25 PM
VBA convert day and date from text string to Excel date Max Bialystock[_2_] Excel Programming 5 May 14th 07 04:54 AM
changing a string date into a 'date' DowningDevelopments Excel Programming 4 December 9th 05 05:00 PM
Converting a string date into a Excel Date Phillips Excel Programming 0 November 24th 03 08:54 PM


All times are GMT +1. The time now is 07:48 AM.

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"