Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,389
Default Extracting date from cell

I have a text in a cell which ends with a date. I would like to extract the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Extracting date from cell

give an example of the format of the date

mmddyyyy
yyyymmdd

--


Gary Keramidas
Excel 2003


"Fred Smith" wrote in message
...
I have a text in a cell which ends with a date. I would like to extract the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 896
Default Extracting date from cell

maybe you meant sth. like

ReportDate = Right(Range("L3"),8) - 1

?

On 9 Kwi, 06:22, "Fred Smith" wrote:
I have a text in a cell which ends with a date. I would like to extract the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default Extracting date from cell

Sub Dte()
Dim ReportDate As Date, MyStr As String
MyStr = Range("L3")
ReportDate = Right(MyStr, 8)
Range("M3") = ReportDate - 1 'subtract 1 day
End Sub

This will put the date minus 1 day, into the cell to the right (M3).

Mike F
"Fred Smith" wrote in message
...
I have a text in a cell which ends with a date. I would like to extract the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Extracting date from cell

On Thu, 8 Apr 2010 21:22:58 -0700, "Fred Smith" wrote:

I have a text in a cell which ends with a date. I would like to extract the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred


CDate(Right(Range("L3").Value, 8)) - 1

This assumes that the date format in the string is mm/dd/yy

VBA can get funny with non-US date formats.
--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,389
Default Extracting date from cell

Thanks, Ron. That's the one that worked for me.

Fred.

"Ron Rosenfeld" wrote in message
...
On Thu, 8 Apr 2010 21:22:58 -0700, "Fred Smith"
wrote:

I have a text in a cell which ends with a date. I would like to extract
the
date. I tried the following:

Dim ReportDate As Date
ReportDate = Range(Right("L3",8)) - 1

but got a type mismatch error.

How do I extract the last 8 characters of a cell into a Date variable in
VBA?

Thanks,
Fred


CDate(Right(Range("L3").Value, 8)) - 1

This assumes that the date format in the string is mm/dd/yy

VBA can get funny with non-US date formats.
--ron


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Extracting date from cell

On Fri, 9 Apr 2010 17:06:38 -0700, "Fred Smith" wrote:

Thanks, Ron. That's the one that worked for me.

Fred.


Glad to help.
Thanks for the feedback.
--ron
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Extracting date from cell

On Fri, 9 Apr 2010 17:06:38 -0700, "Fred Smith" wrote:

Thanks, Ron. That's the one that worked for me.

Fred.


By the way, there are other methods that can work also, but the one I posted
seemed the best "self-documenting" one for me.

You could, of course, dim a variable of type Date, and then set the cell equal
to that variable. That would also force the conversion if the cell contents
were text. But, it's not as obvious that is what is going on.
--ron
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
Extracting date from Date and Time formatted cell Aviral Sharma Excel Discussion (Misc queries) 2 March 6th 09 05:04 AM
Extracting Year from date cell Bob Excel Programming 4 August 30th 07 03:46 PM
Extracting the Day from a date Eqa Excel Discussion (Misc queries) 3 May 29th 07 01:54 AM
Extracting Time from a cell that has both the date and the time Hani Muhtadi Excel Discussion (Misc queries) 3 September 9th 05 10:59 AM
Extracting the Month from the Date in other cell RustyR Excel Programming 3 December 15th 04 03:11 PM


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

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

About Us

"It's about Microsoft Excel"