VBA - 生日前10天的msgbox

时间:2016-05-24 19:57:37

标签: excel vba msgbox

在B栏的sheet1中我有名字在D栏的表1中我有生日日期

B:
Ben
Mikael

D:
3 MARCH 1987
3 JUNE 1976

我想找一个VBA脚本来制作一个弹出式msgbox并打印出“有10天或更短的时间,直到NAME(来自B栏)都有生日”。

如何做到这一点?

2 个答案:

答案 0 :(得分:1)

E1 中输入:

=DATE(YEAR(D1)+DATEDIF(D1,TODAY(),"y")+1,MONTH(D1),DAY(D1))

并复制下来。然后在 F1 中输入:

=E1-TODAY()

并复制下来。

E 是即将到来的生日,列 F 是下一个生日的天数。

最后在工作表代码区域中,输入以下事件宏:

Private Sub Worksheet_Activate()
    Dim cel As Range, F As Range

    Set F = Intersect(ActiveSheet.UsedRange, Range("F:F"))
    For Each cel In F
        If cel.Value < 11 Then
            MsgBox cel.Offset(0, -4).Value & " will have a birthday in " & cel.Value & " days"
        End If
    Next cel
End Sub

每当激活工作表时,您都会收到消息:

enter image description here

答案 1 :(得分:0)

我想我找到了一种方法来做我想做的事。

感谢您的亲切赞扬。

以下是我开始工作的代码。但是为了它的工作,我必须首先采取生日,并将其分为三个单元格,日,月和年。 然后我再次结合日和月而不是年份,而是使用了年份(now())并获得了今年的生日。

然后我可以使用下面的代码在dateadd中添加1-10天。

这感觉有点笨拙......但到目前为止,我认为它会起作用。

Sub checkearlybirthday()
Dim ans As String
Dim cell As Range

For Each cell In Sheets("Personal").Range("rf10:rf500")
    If cell = DateAdd("d", 1, Date) Or cell = DateAdd("d", 2, Date) Or cell = DateAdd("d", 3, Date) Or cell = DateAdd("d", 4, Date) Or cell = DateAdd("d", 5, Date) Or cell = DateAdd("d", 6, Date) Or cell = DateAdd("d", 7, Date) Or cell = DateAdd("d", 8, Date) Or cell = DateAdd("d", 9, Date) Or cell = DateAdd("d", 10, Date) Then
        ans = ans & vbNewLine & Sheets("Personal").Cells(cell.Row, 2).Value
    End If
Next

If Len(ans) < 1 Then

Else
    MsgBox "Hallå! " & vbNewLine & "Det är 10 dagar eller mindre till dessa fyller år. " & ans & "  " & vbNewLine & ""
End If

End Sub

相关问题