Excel VBA 根据单元格日期计算周数并显示
Excel VBA to calculate week number from cell date and display
我有一个 excel vba sub,它根据单元格中的日期每 14 天发送一次电子邮件提醒。我还想包括从单元格中的日期到今天的日期的周数。例如。电池日期 4 月 1 日到现在的 4 月 28 日到 return 4 周。请有人帮忙。
Sub SalesProgress14()
'
' 14 Day Sales Chase Loop
'
'Dim Answer As VbMsgBoxResult
'Answer = MsgBox("Are you sure you want to run?", vbYesNo, "Run Macro")
'If Answer = vbYes Then
Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Dim saledate As String
Set Mail_Object = CreateObject("Outlook.Application")
For i = 2 To lr
With Mail_Object.CreateItem(o)
.Subject = "Sales Chase" & Range("S" & i).Value & " " & Range("U" & i).Value & " " & Range("G" & i).Value
.To = "test@test.com"
.Body = Range("S" & i).Value & " " & Range("U" & i).Value & " " & Range("G" & i).Value
'.display
' Our data below
saledate = Range("F" & i).Value
' Send logic
If DateDiff("d", saledate, Date) Mod 14 = 0 Then .Send
If saledate = Date - 7 Then .Send
End With
Next i
'MsgBox "E-mails successfully sent", 64
'Application.DisplayAlerts = False
Set Mail_Object = Nothing
' The End If below relates to the run yes or no box
'End If
End Sub
使用
DateDiff("w", saledate, Date)
您可以使用 ISOWEEKNUM
Public Sub Test()
Dim saleDate As Date, currDate As Date
saleDate = "2018-04-01"
currDate = "2018-04-28"
Debug.Print Application.WorksheetFunction.IsoWeekNum(currDate) - Application.WorksheetFunction.IsoWeekNum(saleDate)
End Sub
我有一个 excel vba sub,它根据单元格中的日期每 14 天发送一次电子邮件提醒。我还想包括从单元格中的日期到今天的日期的周数。例如。电池日期 4 月 1 日到现在的 4 月 28 日到 return 4 周。请有人帮忙。
Sub SalesProgress14()
'
' 14 Day Sales Chase Loop
'
'Dim Answer As VbMsgBoxResult
'Answer = MsgBox("Are you sure you want to run?", vbYesNo, "Run Macro")
'If Answer = vbYes Then
Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Dim saledate As String
Set Mail_Object = CreateObject("Outlook.Application")
For i = 2 To lr
With Mail_Object.CreateItem(o)
.Subject = "Sales Chase" & Range("S" & i).Value & " " & Range("U" & i).Value & " " & Range("G" & i).Value
.To = "test@test.com"
.Body = Range("S" & i).Value & " " & Range("U" & i).Value & " " & Range("G" & i).Value
'.display
' Our data below
saledate = Range("F" & i).Value
' Send logic
If DateDiff("d", saledate, Date) Mod 14 = 0 Then .Send
If saledate = Date - 7 Then .Send
End With
Next i
'MsgBox "E-mails successfully sent", 64
'Application.DisplayAlerts = False
Set Mail_Object = Nothing
' The End If below relates to the run yes or no box
'End If
End Sub
使用
DateDiff("w", saledate, Date)
您可以使用 ISOWEEKNUM
Public Sub Test()
Dim saleDate As Date, currDate As Date
saleDate = "2018-04-01"
currDate = "2018-04-28"
Debug.Print Application.WorksheetFunction.IsoWeekNum(currDate) - Application.WorksheetFunction.IsoWeekNum(saleDate)
End Sub