2013-06-18 151 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

相關問題