Vérifiez deux strings ont les mêmes valeurs mois et année

J'ai deux variables BrkPt et TestDate. BrkPt a une valeur de 3/05/2014 et TesDate a une valeur de 201405. Je dois vérifier si ces deux valeurs sont identiques (ce qui est oui dans ce cas – année 2014 et mois 05). Quelqu'un peut-il m'aider à procéder à cela?

For GateChartRowCount = 15 To GateChartLastRow Worksheets("Gate Chart").Activate GateChartValueToFind = Cells(GateChartRowCount, 1).Value If (GateChartValueToFind = vbNullSsortingng) 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 

Si vous êtes sûr de disposer du format BrkPt et TestDate, vous pouvez utiliser le code suivant:

 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 

Si TestDate est une string du type "YYYYMM" et BrkPt est une valeur DateTime, vous pouvez utiliser une fonction pour effectuer votre évaluation:

 Public Function IsTheSame(ByVal BrkPt As Date, ByVal TestDate As Ssortingng) As Boolean Dim str1 As Ssortingng: str1 = Year(BrkPt) & Month(BrkPt) If str1 = TestDate Then IsTheSame = True Else IsTheSame = False End If End Function 

Ainsi, vous pourrez appeler la fonction et faire votre comparaison, la fonction répondra à votre question "sont les deux dates avec le même mois et l'année?" en vous disant "Vrai" ou "Faux".