Newbie: How to construct an array of days in year
Author |
Message |
MJS #1 / 6
|
 Newbie: How to construct an array of days in year
Hi, How do I create an array that spans the first day of the current year to the current day of the current year? So I can use it something like this: Debug.Print MyArray(1) '01/01/2002 Is this possible? Thanks, mjs
|
Wed, 19 Jan 2005 09:00:03 GMT |
|
 |
Duane Bozart #2 / 6
|
 Newbie: How to construct an array of days in year
Quote:
> Hi, > How do I create an array that spans the first day of > the current year to the current day of the current year? > So I can use it something like this: > Debug.Print MyArray(1) '01/01/2002 > Is this possible? > Thanks,
If I ken you want a string array of dates.... Write a function that fills the array using the format() function over a loop---the VB date and time functions are sufficient to return the other information required to determine the number of days, etc., although you may find it useful to build a helper function or two...
|
Wed, 19 Jan 2005 09:04:51 GMT |
|
 |
Phil #3 / 6
|
 Newbie: How to construct an array of days in year
Quote: >Hi, >How do I create an array that spans the first day of >the current year to the current day of the current year? >So I can use it something like this: >Debug.Print MyArray(1) '01/01/2002 >Is this possible? >Thanks, >mjs
This may be a start for you. Private Sub Command1_Click() Dim start Dim i As Integer start = DateSerial(2002, 1, 1) 'year- month-day For i = 0 To 365 List1.AddItem start + i Next i End Sub Phil
|
Wed, 19 Jan 2005 21:16:10 GMT |
|
 |
David Segal #4 / 6
|
 Newbie: How to construct an array of days in year
Quote:
>Hi, >How do I create an array that spans the first day of >the current year to the current day of the current year? >So I can use it something like this: >Debug.Print MyArray(1) '01/01/2002 >Is this possible? >Thanks, >mjs
The following will achieve this: ' Dimension the array by calculating the number of days so far ReDim MyArray(DatePart("y", Date)) As Date For nI = 1 To UBound(MyArray) ' Subtract the number of days from today for this location MyArray(nI) = DateAdd("d", -(UBound(MyArray) - nI), Date) Next nI However, since you know the date in any element of the array by it's location, you can probably dispense with the date in the array. If you first calculate the last day of last of year as: dtDateZero = DateSerial(Year(Date) - 1, 12, 31) then you can your Debug.Print becomes Debug.Print DateAdd("d", 1, dtDateZero) Since you are a self-confessed newbie I should add that, contrary to the above code, all variables should be declared and you should not recalculate invariant expressions within a loop. Then you can Debug.Print
|
Wed, 19 Jan 2005 23:47:22 GMT |
|
 |
Dan #5 / 6
|
 Newbie: How to construct an array of days in year
Going right along with the previous post, you could write a simple method that will return exactly what your array would hold, but would take up no memory and would have no initialization needs. Public Function SimulatedDateArray(DayNumber) As Date MyDateArray = DateAdd("d", DayNumber, DateSerial(Year(Now), 1, 0)) End Function You can then call Debug.Print MySimulatedDateArray(1) ' 1/1/2002 Works for me.
Quote:
> >Hi, > >How do I create an array that spans the first day of > >the current year to the current day of the current year? > >So I can use it something like this: > >Debug.Print MyArray(1) '01/01/2002 > >Is this possible? > >Thanks, > >mjs > The following will achieve this: > ' Dimension the array by calculating the number of days so far > ReDim MyArray(DatePart("y", Date)) As Date > For nI = 1 To UBound(MyArray) > ' Subtract the number of days from today for this location > MyArray(nI) = DateAdd("d", -(UBound(MyArray) - nI), Date) > Next nI > However, since you know the date in any element of the array by it's > location, you can probably dispense with the date in the array. If you > first calculate the last day of last of year as: > dtDateZero = DateSerial(Year(Date) - 1, 12, 31) > then you can your Debug.Print becomes > Debug.Print DateAdd("d", 1, dtDateZero) > Since you are a self-confessed newbie I should add that, contrary to > the above code, all variables should be declared and you should not > recalculate invariant expressions within a loop. > Then you can > Debug.Print
|
Thu, 20 Jan 2005 01:58:06 GMT |
|
 |
mjs #6 / 6
|
 Newbie: How to construct an array of days in year
Thanks to everyone :~) mjs
|
Sat, 22 Jan 2005 00:49:37 GMT |
|
|
|