Excel VBA – 将单元格中的date与当前date进行比较
(Excel 2010 VBA)我有一个单元格(A1),其格式为mmm-yy(“Custom”类别)。 举个例子,如果我input的是1/6/13,那么这个单元格显示的是6月13日。 没关系。 在我的VBmacros中,我需要检查这个date,月份是否是当前月份以及年份是否是当前年份。 我不在乎这一天。
这是否帮助你:
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
这个怎么样:
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
如果月份和年份与当前date相同,则函数返回true。 如果不是,则返回false。
感谢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
那做了这个工作!
非常感谢大家,
加迪