views:

37

answers:

3

I'm new to programming in VBA, but what I'm trying to do right now is have a PowerPoint slide that updates every day. It's a weather forecast slide that is displayed in our lobby, and currently I manually update the seven day forecast each day I come in. This means that until I come in, it shows the current day as yesterday's date, and the seven day forecast is still showing yesterday's date in the forecast. Ultimately I'd like to have it pull in the weather data automatically as well, but for the scope of this question, I'm just trying to figure out how to display the dates in a textbox for the seven days of the week.

Basically, there's a header along the top with: (e.g. Wednesday, June 30, 2010)

Then the seven days set up in columns with: (e.g. June 30 July 1 July 2 ...)

I would like to set the header to the current date as shown, and then the seven textboxes below to the current day, then tomorrow, then the next day... and so on until the seventh day.

How would I increment the DateTime? Thanks!

+3  A: 

If you want to go forward in 1-day steps, you can increment DateTime variables by adding to them:

Dim d As DateTime
d = Now()

d = d + 1 ''# => tomorrow

This works because internally, a DateTime is represented as a floating point number with whole days before the comma and fractions of days after the comma. (Consequently, adding 0.5 would effectively add 12 hours, though I would not recommend doing that.)

For more complex operations like adding months or hours, there is DateAdd() (see MSDN).

d = DateAdd("h", 12, d)  ''#=> 12 hours

You can "add" negative values with DateAdd(), too.

Tomalak
I appreciate the help from everyone! I'm going with this answer since it's closest to what I need in this instance.
kcoppock
+2  A: 

These links should help a bit.

  1. Dates And Times In Excel;
  2. Date & Time.

The Now() function, if you want to get the date along with the time. Refer to the linked articles provided for the adds and subtracts. Date() should get you only the date, if you ever happen to be interested only into the date of the day.

Here's an interesting article about the VBA functions.

Understanding VBA Functions and Their Uses

Will Marcouiller
+3  A: 

Here is it in Excel; the VBA function names will be the same. The Format and DateAdd functions are specifically what you're looking for.

Public Sub writeDates()
Dim x As Date, i As Integer
    x = Now
    For i = 1 To 7
       ThisWorkbook.Worksheets(1).Range("A" & i).Value = Format(DateAdd("d", i, x), "dddd, mmmm dd, yyyy")
    Next
End Sub
Tahbaza