View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Transferring a Date array to a range

It's not the assignment back to the worksheet--it's your function.

If the old date isn't March 25, 2006, then the ChangeDate will be 0.

Maybe you wanted:

Option Explicit
Function ChangeDate(OldDate As Variant) As Date
If OldDate = #3/25/2006# Then
ChangeDate = #5/11/2007#
Else
ChangeDate = OldDate
End If
End Function

Sub DateArrayTest()
Dim j As Long

Dim NewDateArray() As Date
ReDim NewDateArray(1 To 3, 1 To 1)
Dim OldDateArray As Variant
OldDateArray = Range("A1").Resize(3, 1)

For j = 1 To 3
NewDateArray(j, 1) = ChangeDate(OldDateArray(j, 1))
Next j

Range("B1").Resize(1, 1) = NewDateArray(1, 1) 'this works
Range("C1").Resize(3, 1) = NewDateArray 'but this doesn't!

End Sub

jonigr wrote:

I am having trouble transferring a €śDate array€ť to a range.

In my XL sheet cells A1-A3 are pre-formatted as mm/dd/yyyy dates.

MyDateArray is a two dimensional (3, 1) array containing dates, say all
three are 05/11/2007.

When I try

Range("A1").Resize(1, 1) = MyDateArray(1, 1)

this works perfect and puts

05/11/2007

in A1, but

Range("A1").Resize(3, 1) = MyDateArray

gives me three times

01/00/1900
01/00/1900
01/00/1900

in A1-A3.

This is actually a much longer array and I prefer not to loop through all
the elements. Is there a way to transfer it to the sheet at once without
disturbing the dates? Thanks very much!


--

Dave Peterson