Formule fin de mois VBA

Bonjour, j'ai un code VBA qui masque des lignes si la date est supérieur au 20 du mois.

J'aimerais que le nouveau code masque les lignes supérieur à la date de fin de mois par rapport à la cellule A3.

Par exemple :

  • Si A3=01/01/23 alors la fin de mois est le 31/01/23 et pas de masquage de cellules
  • Si A3=01/02/23 alors la fin de mois est le 28/02/23 alors on masque les lignes 43-44-45
  • Si A3=01/03/23 alors la fin de mois est le 31/01/23 et pas de masquage de cellules
  • Si A3=01/04/23 alors la fin de mois est le 30/04/23 alors on masque les lignes 45

Et ainsi de suite.

Merci pour votre aide.

If Not Application.Intersect(Target, Range("A3")) Is Nothing Then
    For i = 32 To 35
        If format(ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 2), "dd") > 20 Then

            ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = True

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 5) = ""

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 6) = ""

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 7) = ""

        Else

            ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = False

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 5).FormulaLocal = "=SI(ET(ESTERREUR(TROUVE(""&"";$D" & i & ";1));ESTERREUR(TROUVE(""|"";$D" & i & ";1)));SIERREUR(RECHERCHEV($D" & i & ";'[MON PLANNING.xlsm]RESPONSABLE'!$A:$K;11;FAUX);"""");SI(ESTERREUR(TROUVE(""&"";$D" & i & ";1));GAUCHE($D" & i & ";TROUVE(""|"";$D" & i & ";1)-2);SI(ESTERREUR(TROUVE(""|"";$D" & i & ";1));SIERREUR(RECHERCHEV(GAUCHE($D" & i & ";SI(ESTERREUR(TROUVE(""&"";$D" & i & ";1));TROUVE(""&"";$D" & i & ";1)-2;TROUVE(""&"";$D" & i & ";1)-2));'[MON PLANNING.xlsm]RESPONSABLE'!$A:$K;11;FAUX);"""");GAUCHE($D" & i & ";TROUVE(""&"";$D" & i & ";1)-2))))"

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 6).FormulaLocal = "=SI(ESTERREUR(TROUVE(""|"";$D" & i & ";1));SIERREUR(RECHERCHEV($D" & i & ";'[MON PLANNING.xlsm]RESPONSABLE'!$K:$K;1;FAUX);$D" & i & ");GAUCHE($D" & i & ";TROUVE(""|"";$D" & i & ";1)-2))"

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 7).FormulaLocal = "=SI(ESTERREUR(TROUVE(""|"";D" & i & ";1));SIERREUR(RECHERCHEV(SI(ESTERREUR(TROUVE(""&"";D" & i & ";1));$D" & i & ";GAUCHE($D" & i & ";TROUVE(""&"";D" & i & ";1)-2));'[MON PLANNING.xlsm]RESPONSABLE'!$A:$N;14;FAUX);"""");STXT(D" & i & ";TROUVE(""|"";D" & i & ";1)+2;999))"
        End If
    Next
    Sheets("RESPONSABLE").Calculate
End If

Bonjour,

Plutôt que de tester le jour, testes le mois Si le mois n'est pas le même que celui de A3, c'est que le mois comporte moins de 31 jours et tu masques les clignes.

If Not Application.Intersect(Target, Range("A3")) Is Nothing Then
    nMoisRef = Month(Range("A3").Value
    For i = 32 To 35
        If  Month(ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 2).Value) <> nMoisRef Then

            ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = True
...

Bonjour,

Pour abonder dans la proposition de Cyflo (salut), suivant le mois on masque les lignes :

        Select Case Month(Target)
            Case 4, 6, 9, 11 'avril, juin, septembre, novembre
                ThisWorkbook.Worksheets("RESPONSABLE").Rows("45").Hidden = True
                ThisWorkbook.Worksheets("RESPONSABLE").Rows("43:44").Hidden = False
            Case 2 'février
                If Target Mod 4 = 0 Then 'année bissextile
                    ThisWorkbook.Worksheets("RESPONSABLE").Rows("44:45").Hidden = True
                    ThisWorkbook.Worksheets("RESPONSABLE").Rows("43").Hidden = False
                Else 'année non bissextile
                    ThisWorkbook.Worksheets("RESPONSABLE").Rows("43:45").Hidden = True
                End If
            Case Else 'tous les autres mois
                ThisWorkbook.Worksheets("RESPONSABLE").Rows("43:45").Hidden = False
        End Select

Cdlt

Bonjour,

Une autre solution :

Private Sub Worksheet_Change(ByVal Target As Range)
Dim DernJour As Byte
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A3")) Is Nothing And Target <> "" And IsDate(Target) Then
    Rows("43:45").EntireRow.Hidden = False
    DernJour = Day(Application.EoMonth(Target, 0)) + 15
    If DernJour <= 45 Then Rows(DernJour & ":45").EntireRow.Hidden = True
End If
End Sub

Bon appétit

Comment est-il possible de déclarer la valeur nMoisRef dans le code VBA

Bonjour,

Plutôt que de tester le jour, testes le mois Si le mois n'est pas le même que celui de A3, c'est que le mois comporte moins de 31 jours et tu masques les clignes.

If Not Application.Intersect(Target, Range("A3")) Is Nothing Then

nMoisRef = Month(Range("A3").Value

For i = 32 To 35

If Month(ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 2).Value) <> nMoisRef Then

ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = True

...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Nom, Periode As String
Dim Hs_Norm, HS_Ferie, HS_Nuit, Total As Single
Dim DrLigne, A As Integer
Dim Test As Boolean
Application.EnableEvents = False
Application.ScreenUpdating = False
deprotege

If Not Application.Intersect(Target, Range("A3")) Is Nothing Then
    nMoisRef = Month(Range("A3").Value
    For i = 32 To 35

        If Month(ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 2).Value) <> nMoisRef Then

            ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = True

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 5) = ""

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 6) = ""

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 7) = ""

        Else

            ThisWorkbook.Worksheets("RESPONSABLE").Rows(i + 10).Hidden = False

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 5).FormulaLocal = "=SI(ET(ESTERREUR(TROUVE(""&"";$D" & i & ";1));ESTERREUR(TROUVE(""|"";$D" & i & ";1)));SIERREUR(RECHERCHEV($D" & i & ";'[MON PLANNING.xlsm]RESPONSABLE'!$A:$K;11;FAUX);"""");SI(ESTERREUR(TROUVE(""&"";$D" & i & ";1));GAUCHE($D" & i & ";TROUVE(""|"";$D" & i & ";1)-2);SI(ESTERREUR(TROUVE(""|"";$D" & i & ";1));SIERREUR(RECHERCHEV(GAUCHE($D" & i & ";SI(ESTERREUR(TROUVE(""&"";$D" & i & ";1));TROUVE(""&"";$D" & i & ";1)-2;TROUVE(""&"";$D" & i & ";1)-2));'[MON PLANNING.xlsm]RESPONSABLE'!$A:$K;11;FAUX);"""");GAUCHE($D" & i & ";TROUVE(""&"";$D" & i & ";1)-2))))"

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 6).FormulaLocal = "=SI(ESTERREUR(TROUVE(""|"";$D" & i & ";1));SIERREUR(RECHERCHEV($D" & i & ";'[MON PLANNING.xlsm]RESPONSABLE'!$K:$K;1;FAUX);$D" & i & ");GAUCHE($D" & i & ";TROUVE(""|"";$D" & i & ";1)-2))"

            ThisWorkbook.Worksheets("RESPONSABLE").Cells(i, 7).FormulaLocal = "=SI(ESTERREUR(TROUVE(""|"";D" & i & ";1));SIERREUR(RECHERCHEV(SI(ESTERREUR(TROUVE(""&"";D" & i & ";1));$D" & i & ";GAUCHE($D" & i & ";TROUVE(""&"";D" & i & ";1)-2));'[MON PLANNING.xlsm]RESPONSABLE'!$A:$N;14;FAUX);"""");STXT(D" & i & ";TROUVE(""|"";D" & i & ";1)+2;999))"
        End If
    Next
    Sheets("RESPONSABLE").Calculate

Voir ci-dessous. Attention, j'avais oublié la parenthèse fermante dans Month(Range("A3").Value)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Nom, Periode As String
Dim Hs_Norm, HS_Ferie, HS_Nuit, Total As Single
Dim DrLigne, A As Integer
Dim Test As Boolean
Dim nMoisRef as Integer
Application.EnableEvents = False
Application.ScreenUpdating = False
deprotege

If Not Application.Intersect(Target, Range("A3")) Is Nothing Then
    nMoisRef = Month(Range("A3").Value)

...

Un grand merci, ça fonctionne à merveille

Rechercher des sujets similaires à "formule fin mois vba"