2013-06-18 156 views
1

(Excel 2010 VBA) 我有一个单元格(A1),其格式为mmm-yy(“Custom”类别)。例如,如果我输入1/6/13,则单元显示6月13日。没关系。 在我的VB宏中,我需要检查这个日期,月份是否是当前月份以及年份是否是当前年份。我不在乎这一天。Excel VBA - 将单元格中的日期与当前日期进行比较

回答

1

感谢Dave和MiVoth我所做的:

Dim xdate As Date 
xdate = Worksheets("sheet1").Range("A1") 
    If Month(Date) = Month(xdate) And Year(Date) = Year(xdate) Then 
     MsgBox "OK" 
    Else 
     MsgBox "not OK" 
    End If 

那做的工作!
非常感谢大家,
Gadi

+0

确保选择一个作为答案,最好选择其中一个或两个以确保其他类似问题的人可以轻松找到正确的答案。 – jaysoncopes

1

如何:

Function MonthYear() As Boolean 
MonthYear = False 
If Not IsDate(Cells(1, 1)) Then Exit Function 
If Month(Date) = Month(Cells(1, 1)) And Year(Date) = Year(Cells(1, 1)) Then 
    MonthYear = True 
End If 
End Function 

如果年份和月份是一样的当前日期函数返回true。如果不是,则返回false。

4

这是否帮助你:

Public Sub test() 
    d = Sheet1.Range("A1") 
    n = Now() 
    If Year(d) = Year(n) And Month(d) = Month(n) Then 
     MsgBox "It works" 
    End If 
End Sub 
+0

谢谢!这工作! – gadi

相关问题