Excel运行宏时Excel应用时间戳

时间:2016-12-22 15:54:09

标签: excel vba excel-vba macros

我有一个包含To,CC和BCC列的电子邮件列表。当复选框为TRUE时,它会将电子邮件添加到Outlook中的该特定行。

我想要做的是在运行宏时,然后将时间戳应用于单元格中,仅对列#34; 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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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