Récupérer date vba

Résolu/Fermé
happy4u Messages postés 39 Date d'inscription vendredi 28 octobre 2016 Statut Membre Dernière intervention 17 septembre 2017 - Modifié par happy4u le 20/01/2017 à 15:38
happy4u Messages postés 39 Date d'inscription vendredi 28 octobre 2016 Statut Membre Dernière intervention 17 septembre 2017 - 20 janv. 2017 à 17:00
Bonjour,

J'ai un code qui permets de récupérer le fichier le plus récent dans un dossier. Ce fichier est sous format alpha20012017 (alphaDATE).

Est -t-il possible de récupérer la date du ficheir? Merci de votre aide.

Sub FichierLePlusRécent()

Dim rep As String                 'Répertoire
Dim fic As String                 'Fichier
Dim daR As Date                   'Date fichier récent
Dim nom As String                 'Nom fichier récent


rep = "N:\Dossier\" & _
        "Mes_docs\"
  fic = "alpha_*.xls"
  daR = 0
  nom = ""
  fic = Dir(rep & fic)
  Do While fic <> ""
    If daR < FileDateTime(rep & fic) Then
      'mémoriser le nom du fichier le plus récent
      daR = FileDateTime(rep & fic)
      nom = fic
    End If
    fic = Dir
  Loop
'  If nom <> "" Then
'    MsgBox nom
'    Workbooks.Open rep & "\" & nom
'  End If

If nom <> "" Then
    MsgBox nom '' je voudrai récupérer la date pas le nom

End If

Workbooks.Open rep & "\" & nom



End Sub

2 réponses

Patrice33740 Messages postés 8556 Date d'inscription dimanche 13 juin 2010 Statut Membre Dernière intervention 2 mars 2023 1 775
20 janv. 2017 à 15:32
Re,

Merci de baliser le code :


0
happy4u Messages postés 39 Date d'inscription vendredi 28 octobre 2016 Statut Membre Dernière intervention 17 septembre 2017
20 janv. 2017 à 15:38
C'est bon, il est balisé
Merci
0
Patrice33740 Messages postés 8556 Date d'inscription dimanche 13 juin 2010 Statut Membre Dernière intervention 2 mars 2023 1 775
Modifié par Patrice33740 le 20/01/2017 à 15:58
Bonjour,

Par exemple :
  If nom <> "" Then
    str = Split(nom, ".")(UBound(Split(nom, ".")) - 1)
    str = Split(str, "-")(UBound(Split(str, "-")))
    str = Left(str, 2) & "/" & Mid(str, 3, 2) & "/" & Mid(str, 5)
    MsgBox CDate(str)
  End If

EDIT : si la date est précédée d'un -
et si c'est un _ :
  If nom <> "" Then
    str = Split(nom, ".")(UBound(Split(nom, ".")) - 1)
    str = Split(str, "_")(UBound(Split(str, "_")))
    str = Left(str, 2) & "/" & Mid(str, 3, 2) & "/" & Mid(str, 5)
    MsgBox CDate(str)
  End If

Cordialement
Patrice
0
happy4u Messages postés 39 Date d'inscription vendredi 28 octobre 2016 Statut Membre Dernière intervention 17 septembre 2017
20 janv. 2017 à 17:00
ça mache nickel ;)
3éme topic résolu grâce à toi. Merci pour tes efforts
0