开发者

Set textbox text to Today/Tomorrow/Next Day/etc. automatically?

开发者 https://www.devze.com 2023-01-05 18:51 出处:网络
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 m

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 开发者_如何学Goon until the seventh day.

How would I increment the DateTime? Thanks!


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.


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


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


if your still interested in doing this let me know as ive now completed the same thing. i use an addin that gives me automation events such as slide change that work as pps as the inbuilt ones do not work once converted to pps (for auto boot of pc and auto load of presentation), thats called autoevents, google it or email me for the file. From that i then update a shape with (you can then use a nameit addin to name the shapes for easyness or use the imediate window of vba to keep changing shape numbers until you find the right shame (pain in 2003).... my slides change every 20 seconds so i update the time without the seconds into the shape.

i have other bells n whistles like reading to/from dates with a text tag line for saying welcome to my bloggs on certains days etc.

eg for time updates...done on autoevent of slide change ActivePresentation.SlideMaster.Shapes(3).TextFrame.TextRange.Text = Format(Now, "dddd dd mmmm yyyy") & ". " & Format(Now, "hh:mm")

0

精彩评论

暂无评论...
验证码 换一张
取 消