vba 将文本框文本设置为今天/明天/第二天/等。自动地?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3150823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:48:32  来源:igfitidea点击:

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

datetimevbapowerpointpowerpoint-vba

提问by Kevin Coppock

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.

我是 VBA 编程的新手,但我现在想要做的是有一个每天更新的 PowerPoint 幻灯片。这是一个显示在我们大厅里的天气预报幻灯片,目前我每天进来的时候都会手动更新 7 天的预报。这意味着在我进来之前,它将当天显示为昨天的日期,而 7 天的预报是仍然在预测中显示昨天的日期。最终,我也想让它自动提取天气数据,但就这个问题的范围而言,我只是想弄清楚如何在文本框中显示一周中 7 天的日期。

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

基本上,顶部有一个标题:(例如,2010 年 6 月 30 日,星期三)

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

然后在列中设置 7 天:(例如 6 月 30 日 7 月 1 日 7 月 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!

我将如何增加日期时间?谢谢!

回答by Tomalak

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

如果您想以 1 天的步骤前进,您可以通过添加 DateTime 变量来增加它们:

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.)

这是有效的,因为在内部,DateTime 表示为一个浮点数,在逗号之前是整天数,在逗号之后是天数的小数部分。(因此,添加 0.5 将有效地增加 12 小时,但我不建议这样做。)

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

对于更复杂的操作,例如添加月数或小时数,有DateAdd()请参阅 MSDN)。

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

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

您也可以使用 来“添加”负值DateAdd()

回答by Tahbaza

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.

这是在 Excel 中;VBA 函数名称将相同。Format 和 DateAdd 函数正是您所需要的。

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

回答by Will Marcouiller

These links should help a bit.

这些链接应该会有所帮助。

  1. Dates And Times In Excel;
  2. Date & Time.
  1. Excel 中的日期和时间
  2. 日期和时间

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.

Now() 函数,如果您想获取日期和时间。请参阅为加减提供的链接文章。Date()应该只给你日期,如果你碰巧只对当天的日期感兴趣。

Here's an interesting article about the VBA functions.

这是一篇关于 VBA 函数的有趣文章。

Understanding VBA Functions and Their Uses

了解 VBA 函数及其用途

回答by mark

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.

如果您仍然有兴趣这样做,请告诉我,因为我现在完成了同样的事情。我使用一个插件,它为我提供自动化事件,例如幻灯片更改,它可以作为 pps 工作,因为内置的事件一旦转换为 pps(用于 PC 自动启动和演示文稿的自动加载)就不起作用,这就是所谓的自动事件,谷歌它或给我发电子邮件为文件。从那以后,我更新了一个形状(然后您可以使用 nameit 插件来为形状命名以方便使用,或者使用 vba 的中间窗口不断更改形状编号,直到您找到合适的耻辱(2003 年的痛苦)...我的幻灯片每 20 秒更改一次,因此我将时间更新为不带秒数的形状。

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")

例如时间更新...在幻灯片更改的自动事件上完成 ActivePresentation.SlideMaster.Shapes(3).TextFrame.TextRange.Text = Format(Now, "dddd dd mmmm yyyy") & "." & Format(Now, "hh :毫米”)