我有两个变量BrkPt和TestDate。 BrkPt的值为3/05/2014,TesDate的值为201405.我需要检查这两个值是否相同(在本例中为是 - 2014年和05年)。有人可以帮我继续吗?
For GateChartRowCount = 15 To GateChartLastRow
Worksheets("Gate Chart").Activate
GateChartValueToFind = Cells(GateChartRowCount, 1).Value
If (GateChartValueToFind = vbNullString) Then GoTo lblDoneWithValueToFind:
Worksheets("Reference").Activate
RefRowCount = Application.WorksheetFunction.CountIf(Columns(10), GateChartValueToFind)
If (RefRowCount <> 0) Then
RefRowForData = Application.WorksheetFunction.Match(GateChartValueToFind, Columns(10), 0)
BrkPt = Worksheets("Reference").Cells(RefRowForData, 11)
End If
For GateChartColumnCount = 2 To GateChartLastColumn - 3
Worksheets("Gate Chart").Activate
TestDate = Worksheets("Gate Chart").Cells(14, GateChartColumnCount)
TestDateConverted = CDate(TestDate)
BrkPt = Format(BrkPt, "mmyyyy")
BrkPt = CDate(BrkPt)
If TestDateConverted = BrkPt Then
Worksheets("Gate Chart").Cells(GateChartRowCount, GateChartColumnCount + 1) = "YES"
End If
Next GateChartColumnCount
lblDoneWithValueToFind:
Next GateChartRowCount
答案 0 :(得分:0)
如果TestDate
是“YYYYMM”类型的字符串且BrkPt
是DateTime值,那么您可以使用函数进行评估:
Public Function IsTheSame(ByVal BrkPt As Date, ByVal TestDate As String) As Boolean
Dim str1 As String: str1 = Year(BrkPt) & Month(BrkPt)
If str1 = TestDate Then
IsTheSame = True
Else
IsTheSame = False
End If
End Function
因此,您可以调用该函数进行比较,该函数将回答您的问题“这两个日期是同一个月和一年吗?”只是告诉你“真实”或“虚假”。
答案 1 :(得分:0)
如果您确定具有BrkPt和TestDate的固定格式,则可以使用以下代码:
Sub TRY()
BrkPt = "3/05/2014"
TestDateConverted = "201405"
BrkPt = "3/05/2014"
Y = Right(BrkPt, 4)
M = Mid(BrkPt, 3, 2)
D = Left(BrkPt, 1)
BrkPt = Format(DateSerial(Y, M, D), "MMYYYY")
TestDateConverted = "201405"
Y = Left(TestDateConverted, 4)
M = Mid(TestDateConverted, 5, 2)
TestDateConverted = Format(DateSerial(Y, M, D), "MMYYYY")
If BrkPt = TestDateConverted Then
MsgBox "Both Matches"
End If
End Sub