tags:

views:

41

answers:

3

This is probably quite simple but I've googled and can't find an answer. I am also just learning VBA (I have done VB.NET etc. etc.)

If the cell the macro is trying to copy is a date I just get a number copied, e.g. 40352 from 23/06/2010

Here is a code snippet, any help most appreciated, thanks:

Sheet5.Range(Cells(rwStartNumber, currentColumn + 1).Address(False, False)) =
     Sheet5.Range(Cells(rwStartNumber, currentColumn).Address(False, False))

Obviously this is in two loops, but that's not where the problem is.

Thanks!

+1  A: 

You see the number because that's how Excel stores dates: you need to change the format of your target cell to be Date and it will display properly.

Stuart Dunkeld
I had an inkling it was something like that. Is there a way to copy the formatting along with the cell data? I've had a search and the Range().Select and then Selection.Copy, Selection.Paste method brings up application errors or somesuch (and I'm not even sure if that would copy the formatting anyway)
Thomas King
@Thomas King Take a look at PasteSpecial method with xlPasteFormats http://msdn.microsoft.com/en-us/library/aa195818%28office.11%29.aspx
belisarius
+1  A: 

Hi,

You can try the following

With Selection
     .PasteSpecial xlPasteValuesAndNumberFormats
End With

Let me know if it works for you,

Kind Regards,

Trefex
A: 

You can copy the property NumberFormat from the source cell, e.g.:

'Following line copies the values...'
ws.Cells(curRow, curCol + 1) = ws.Cells(curRow, curCol)

'And this copies the formats...'
ws.Cells(curRow, curCol + 1).NumberFormat = ws.Cells(curRow, curCol).NumberFormat

(ws is a variable of the type Worksheet, you can assign it e.g. like this:

Dim ws as Worksheet
Set ws = Worksheets("Sheet5")

or you can just use ActiveSheet)

martin