macros运行时Excel应用时间戳

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

我想要做的是当macros运行,然后将时间戳应用于单元格中的“J”列中的TRUEcheckbox。

这是我正在与之合作。

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 

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

如果您希望时间戳为文本,请先格式化列(或在您的代码中)。 如果列J未格式化为文本,则时间戳将是实际的date时间,并将以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 

尝试

 If .Cells(emailCell.Row, "J").Text = "TRUE" Then sendTo = sendTo & "; " & emailCell.Text .cells(emailcell.row, column).value = hour(now) & ":" & minute(now) End If 
 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