Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA: Copy a date and paste as text

Cell A1 contains a date formatted as follows: 11/4/2009

I want to write a VBA procedure that will read the date in A1 and
insert it as text in B1 in the format 2009-11-04.

I can accomplish this easily in excel by inputting the following
function into B1: =TEXT(A1,"yyyy-mm-dd"). I would then copy the result
and Paste Special Values to get the intended result of text that
reads 2009-11-04.

I tried to replicate this in VBA using the following statement:
Range("B1").Value = worksheetfunction.Text(Range("A1"), "yyyy-mm-dd")

However, the value returned in B1 is still formatted as a date and
appears as 11/4/2009.

Can anyone help with this? This is a simple example but I want to use
this code as part of a longer procedure.

Thanks!
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default VBA: Copy a date and paste as text

Hi Aaron,

Don's answer reformats the date using the number format. Perhaps that is all
that you require. However, if you really want the result as text in lieu of a
date then you need to format the cell first as text because Excel interprets
dates entered as y/m/d as real dates when entered in a cell formatted to the
default General fromat. Also the Format function is the VBA equivalent of
worksheet function TEXT.

Range("B1").NumberFormat = "@"
Range("B1") = Format(Range("A1"), "yyyy-mm-dd")

--
Regards,

OssieMac


"Aaron Rubinstein" wrote:

Cell A1 contains a date formatted as follows: 11/4/2009

I want to write a VBA procedure that will read the date in A1 and
insert it as text in B1 in the format 2009-11-04.

I can accomplish this easily in excel by inputting the following
function into B1: =TEXT(A1,"yyyy-mm-dd"). I would then copy the result
and Paste Special Values to get the intended result of text that
reads 2009-11-04.

I tried to replicate this in VBA using the following statement:
Range("B1").Value = worksheetfunction.Text(Range("A1"), "yyyy-mm-dd")

However, the value returned in B1 is still formatted as a date and
appears as 11/4/2009.

Can anyone help with this? This is a simple example but I want to use
this code as part of a longer procedure.

Thanks!
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA: Copy a date and paste as text

OssieMac,

That's exactly what I was looking for. Thanks for explaining... makes
sense now!

Aaron

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
Date changes from copy to paste Nathan Excel Discussion (Misc queries) 1 December 14th 09 01:30 PM
Copy text from cell and paste it in a separate text box (contents tobe linked) andreashermle Excel Programming 7 December 13th 09 07:51 PM
Copy and Paste + erasing date KU Excel Programming 1 November 23rd 08 02:37 PM
date copy paste should be very easy berk Excel Discussion (Misc queries) 3 August 23rd 07 10:50 AM
Date copy, Paste error Iwojimajim Excel Discussion (Misc queries) 2 January 22nd 07 11:07 PM


All times are GMT +1. The time now is 01:43 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"