【问题标题】:Excel apply timestamp when macro was ranExcel 在运行宏时应用时间戳
【发布时间】:2017-05-08 07:24:36
【问题描述】:

我有一个包含收件人、抄送和密送列的电子邮件列表。当复选框为 TRUE 时,它会将电子邮件添加到 Outlook 中的特定行。

我想做的是在运行宏时将时间戳应用于单元格,仅用于“J”列中的 TRUE 复选框。

这就是我正在使用的。

Sub SendEmail()

' Set up outlook objects for emailing

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

' Body text for the email
Dim strbody As String
strbody = ""

' Strings to contain the email addresses
Dim sendTo As String
sendTo = ""
Dim sendCC As String
sendCC = ""
Dim sendBCC As String
sendBCC = ""

' The cell containing the email address (loop variable)
Dim emailCell As Range

With ActiveSheet

    ' Cycle through email addresses, from B3 to one before next blank cell in column
    For Each emailCell In .Range("B3", .Range("B3").End(xlDown))

        ' Check each TRUE/FALSE column in same row, add email addresses accordingly

        If .Cells(emailCell.Row, "E").Text = "TRUE" Then

            sendTo = sendTo & "; " & emailCell.Text

        End If

        If .Cells(emailCell.Row, "G").Text = "TRUE" Then

            sendCC = sendCC & "; " & emailCell.Text

        End If

        If .Cells(emailCell.Row, "I").Text = "TRUE" Then

            sendBCC = sendBCC & "; " & emailCell.Text

        End If

    Next emailCell

End With

' Generate email in outlook objects defined above
On Error Resume Next
With OutMail
    .To = sendTo
    .CC = sendCC
    .BCC = sendBCC
    .Subject = ""
    .HTMLBody = strbody
    .Display
    ' If you don't want to display the email before sending it,
    ' simply use .Send instead of .Display
End With
On Error GoTo 0
End Sub

【问题讨论】:

  • 您谈论的是复选框,但您的代码涉及单元格中的 TRUE 值。是否有复选框。如果不是,并且单元格正在触发动作,如果三个单元格中只有一个为真,是否会触发?时间戳应该有多详细:天?第二?还有什么?
  • 我只是将其更改为一个侧翼代码,当单元格被触摸时,它会在两个侧翼字符之间切换,看起来像一个未选中的框和一个选中的框。现在可以检查任何人,它将在该位置添加该电子邮件。这样您就可以将其发送给自己以及自己抄送和密送。我正在寻找宏运行的当前日期和时间。谢谢
  • 如果您“查找当前运行宏的日期和时间”,那么它与您所谓的“复选框”的值无关。只需选择一个单元格并输入类似 =now() 的内容,pastevalue 那个单元格。
  • @EEM,OP 希望在运行时添加时间戳。您的解决方案似乎很好,但是您关于 OP 不应使用复选框来确定是否为该行添加时间戳的声明对我来说似乎是错误的。
  • @DougGlancy,OP 说只是想知道宏何时运行,这意味着无论结果如何,所以不需要检查其他任何东西......

标签: vba excel macros


【解决方案1】:

试试

If .Cells(emailCell.Row, "J").Text = "TRUE" Then
    sendTo = sendTo & "; " & emailCell.Text
   .cells(emailcell.row, column).value = hour(now) & ":" & minute(now)
End If 

【讨论】:

  • 我可能理解不正确,但我认为如果该行中的 E、G 或 I 为 TRUE,则 OP 希望在 J 列中添加时间戳。
  • @DougGlancy 你是对的。如果检查了来自 E3、G3 或 I3 的电子邮件,则需要在 J3 中填写时间戳。 (第 3 行是一个例子)
  • @EEM,是什么让它不稳定?它是更大的 Sub 的一部分,即 OP 发布的代码。
  • @DougGlancy Ooops,对不起,我的错误,我不知何故(错误地)将其视为公式,而不是实际值。道歉....
【解决方案2】:

如果 E、G 或 I 三列中的任何一列对于该行为 TRUE,则会添加时间戳。时间戳按从年到秒的降序排列。你可以通过改变格式来改变它。

如果您希望时间戳为文本,请先格式化列(或在您的代码中)。如果列 J 未格式化为文本,则时间戳将是实际的 DateTime,并且将以 Excel 认为的任何格式显示。

If .Cells(emailCell.Row, "E") OR .Cells(emailCell.Row, "G") OR .Cells(emailCell.Row, "I") Then
   .Cells(emailCell.row, "J").Value = Format(Now(), "yyyy-mm-dd hh:mm:ss")
End If

【讨论】:

  • 如果输入为数字,则无需使用Format(Now(), "yyyy-mm-dd hh:mm:ss").Value = Now() 产生相同的结果,作为文本输入的一种方法是在公式中添加“Chr(39) &”(即` = Chr(39) & Format(Now(), "yyyy-mm-dd hh:mm:ss")`
  • 谢谢,但不确定将其放在我的代码中的哪个位置?我尝试过的大多数地方都会给我一个错误“编译错误:无效或不合格的参考”,然后它会突出显示第一个“.Cells”。如果我将它放在 "IF .Cells ... Row, "I") 之后和该对象的结束 "End If" 语句之前,那么原始代码可以工作,但没有时间戳。
  • 你可以把它放在 For/Next 循环的末尾,在最后一个 IF 之后和 Next 之前。我意识到我留下了关于“SendTo”的部分,这是重复的,所以删除了它。但是,这并不影响它。
  • @DougGlancy 我必须在每个 emailcell.row 语句的末尾添加一个 .Text = "R" 。那么它就像一个魅力,非常感谢:)
  • 所以这些值实际上不是 TRUE 或 FALSE?知道这会很有帮助:-)。无论如何,不​​客气,我很高兴它成功了。
【解决方案3】:
If .Cells(emailCell.Row, "D").Text = "R" Or .Cells(emailCell.Row, "E").Text = "R" Or .Cells(emailCell.Row, "F").Text = "R" Then
   .Cells(emailCell.Row, "G").Value = Format(Now(), "dd-mm-yyyy hh:mm AM/PM")
End If

【讨论】:

  • 我没有对此投反对票,但是您的问答测试不同的值是有问题的:TRUE 和“R”。此外,这可能没有必要,因为主要逻辑和使用“R”而不是 TRUE 的讨论在我接受的答案的 cmets 中得到解决。我会考虑删除这个。
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 2013-12-10
  • 2017-12-09
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多