comparaison entre 2 dates

P

philippe69

Guest
Bonjour le Forum

je reviens sur le forum avec une question que je pensais réglée, mais suite à un manque de préçisions de ma part, forcément cela ne correspondait pas à mes souhaits. Je vais donc essayer d'être plus préçis.

Voilà comme on peut le voir dans le code ci-dessous, j'ai une texbox5 dans laquelle je rentre une date. Si celle-ci est valide, elle s'ajoute automatiquement à la suite de la dernière ligne non vide de la colonne A.

Mon souhait est que lorsque l'on tape cette date, si celle-ci est supérieure d'un an exactement à une date située en A11 de ma feuille 3 un msgbox apparaît et exit sub.
Dans le cas contraire, la procédure se poursuit normalement selon les conditions de la macro ci-dessous

Merci pour tous ceux qui voudront bien se pencher sur mon pb

Amicalement
Philippe


Code:
Private Sub CommandButton2_Click()
Dim LastLine As Long

If Not IsDate(Me.TextBox5) Then
    MsgBox 'Vous devez indiquer une date valide'
    Exit Sub
    Else
TheDateControler Me.TextBox5
End If

    If TextBox5 = Empty Then
        UserForm4.Show
    ElseIf TextBox6 = Empty Then
        UserForm5.Show
    ElseIf TextBox7 = Empty Then
        UserForm6.Show
    Else
    
        With Sheets('Feuil1')
            .Unprotect
            LastLine = .Range('A65536').End(xlUp).Row + 1
            
            With .Range('A' & LastLine)
            .Value = CDate(TextBox5.Value)
            .NumberFormat = 'dd/mm/yyyy'
            End With
            
            .Range('B' & LastLine).Value = Month(TextBox5.Value)
            .Range('C' & LastLine).Value = Day(TextBox5.Value)
            .Range('D' & LastLine).Value = TextBox6.Value
            .Range('E' & LastLine).Value = TextBox7.Value
        End With
        
        TextBox5 = ''
        TextBox6 = ''
        TextBox7 = ''
    MsgBox ('votre relevé a été pris en compte')
    End If
    
Dim L As Integer
With Sheets('feuil1')
L = .Range('a65536').End(xlUp).Row
.Range('A1:E' & L).Sort Key1:=Range('A1'), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub
 

Robert

XLDnaute Barbatruc
Repose en paix
Bonsoir Philippe, bonsoir le forum,

J'ai pas testé mais je pense que ça devrait marcher :


Private Sub CommandButton2_Click()
Dim LastLine As Long
Dim L As Integer
Dim aniv As Date

aniv = DateValue(Sheets('Feuil3').Range('A11')) 'définit la variable aniv

If Not IsDate(Me.TextBox5) Then
MsgBox 'Vous devez indiquer une date valide'
Exit Sub
Else
TheDateControler Me.TextBox5
End If

If TextBox5 = Empty Then
UserForm4.Show
ElseIf TextBox6 = Empty Then
UserForm5.Show
ElseIf TextBox7 = Empty Then
UserForm6.Show
Else

'********************************************************
If Day(DateValue(TextBox5.Value)) = Day(aniv) And _
Month(DateValue(TextBox5.Value)) = Month(aniv) _
And DateDiff('yyyy', DateValue(TextBox5.Value), aniv) = 1 Then
MsgBox 'blabla...'
Exit Sub
End If
'********************************************************

With Sheets('Feuil1')
.Unprotect
LastLine = .Range('A65536').End(xlUp).Row + 1

With .Range('A' & LastLine)
.Value = CDate(TextBox5.Value)
.NumberFormat = 'dd/mm/yyyy'
End With

.Range('B' & LastLine).Value = Month(TextBox5.Value)
.Range('C' & LastLine).Value = Day(TextBox5.Value)
.Range('D' & LastLine).Value = TextBox6.Value
.Range('E' & LastLine).Value = TextBox7.Value
End With

TextBox5 = ''
TextBox6 = ''
TextBox7 = ''
MsgBox ('votre relevé a été pris en compte')

End If

With Sheets('feuil1')
L = .Range('a65536').End(xlUp).Row
.Range('A1:E' & L).Sort Key1:=Range('A1'), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

End Sub

Message édité par: Robert, à: 18/02/2006 23:47
 
P

philippe69

Guest
bonsoir Robert

Je te remercie pour ta solution mais apparement cela ne fonctionne pas.

Du coup, j'ai pensé que ce serait surement plus simple si je joignais un fichier exemple avec ma macro simplifiée [file name=dateaniv.zip size=13187]http://www.excel-downloads.com/components/com_simpleboard/uploaded/files/dateaniv.zip[/file]
 

Pièces jointes

  • dateaniv.zip
    12.9 KB · Affichages: 16

Robert

XLDnaute Barbatruc
Repose en paix
Bonsoir Philippe, bonsoir le forum,

Oui en effet, j'avais inversé les dates et la fonction DateDiff renvoyait -1 au lieu de 1.

En pièce jointe ton code corrigé et remanié pour l'ordre des messages.
[file name=dateaniv2.zip size=14633]http://www.excel-downloads.com/components/com_simpleboard/uploaded/files/dateaniv2.zip[/file]
 

Pièces jointes

  • dateaniv2.zip
    14.3 KB · Affichages: 17

Statistiques des forums

Discussions
312 480
Messages
2 088 757
Membres
103 950
dernier inscrit
Thomas Solioz