[VBA] Rechercher et ouvrir un fichier suivant sa date

Bonjour,

J'ai un répertoire :

strDossier = "\\Issou-servvlt\INDICATEURS_PROD\"

dans lequel figurent des fichiers :

strIndicECRnoeuds = Dir(strDossier & "\*06_NEW_ECR_WIP_V01.xls*", vbNormal)

Je souhaite via une Userform contenant un Bouton ainsi que un DTPikcer ouvrir le fichier "\*06_NEW_ECR_WIP_V01.xls*" créer à la date spécifiée dans le DTPicker.

Voici le code que j'utilisais avant de mettre le DTPicker : (Ce code me permettait d'ouvrir le dernier fichier créer)

Private Sub CommandButton1_Click()

Dim strDossier As String, strIndicECRnoeuds As String
Dim latestECRnoeuds As String, dtLastECR As Date
nomINDICECR As String

'MsgBox "Il y en a : " & DTPicker1.Value, vbOKOnly + vbInformation, "Nb sheet"

strDossier = "\\Issou-servvlt\INDICATEURS_PROD\"
strIndicECRnoeuds = Dir(strDossier & "\*06_NEW_ECR_WIP_V01.xls*", vbNormal)

    Do While strIndicECRnoeuds <> ""
        If FileDateTime(strDossier & strIndicECRnoeuds) > dtLastECR Then
            latestECRnoeuds = strDossier & strIndicECRnoeuds
            dtLastECR = FileDateTime(strDossier & strIndicECRnoeuds)
            latestECRnoeuds = strDossier & strIndicECRnoeuds
        End If
        strIndicECRnoeuds = Dir
    Loop

If Not IsOpen(latestECRnoeuds) Then 'OUVERTURE INDICATEUR'
    Workbooks.Open latestECRnoeuds
Else
End If

'DEPUIS INDICATEUR
nomINDICECR = ActiveWorkbook.Name

End Sub

Du coup au lieu de cherche le dernier fichier j'aimerais ajouté la condition de la date à sélectionner via le DTPicker :

J'ai donc capturé la valeur issue du DTpicker via DTPicker1.Value. Mais je ne parviens pas à ouvrir autre chose que le dernier fichier créé... Pourriez vous m'éclairer s'il vous plait ?

Private Sub CommandButton1_Click()

Dim strDossier As String, strIndicECRnoeuds As String
Dim latestECRnoeuds As String, dtLastECR As Date
nomINDICECR As String
DTPicker1 As Date

'MsgBox "La date selectionné est : " & DTPicker1.Value, vbOKOnly + vbInformation, "Date choisie"

strDossier = "\\Issou-servvlt\INDICATEURS_PROD\"
strIndicECRnoeuds = Dir(strDossier & "\*06_NEW_ECR_WIP_V01.xls*", vbNormal)

    Do While strIndicECRnoeuds <> ""
        'If FileDateTime(strDossier & strIndicECRnoeuds) > dtLastECR Then
        If Format(FileDateTime(strDossier & strIndicECRnoeuds), "dd/mm/yyyy") = DTPicker1.Value Then
            latestECRnoeuds = strDossier & strIndicECRnoeuds
            'dtLastECR = FileDateTime(strDossier & strIndicECRnoeuds)
            'dtLastECR = DTPicker1.Value
            latestECRnoeuds = strDossier & strIndicECRnoeuds
        End If
        strIndicECRnoeuds = Dir
    Loop

If Not IsOpen(latestECRnoeuds) Then 'OUVERTURE INDICATEUR'
    Workbooks.Open latestECRnoeuds
Else
End If

'DEPUIS INDICATEUR
nomINDICECR = ActiveWorkbook.Name

End Sub

Salut Julien, et bienvenue sur ce Forum,

Salut le Forum,

Le code ci-dessous fonctionne chez moi :

Sub FileDate()
Dim objFSO As Scripting.FileSystemObject, Dt As Date, ch, fi As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
ch = "P:\PERSONNEL\Excel\"
Dt = InputBox("Merci de saisir votre date:")
fi = Dir(ch)
Do While fi <> ""
    If FileDateTime(ch & fi) Like Dt & "*" Then MsgBox fi
    fi = Dir
Loop
End Sub

Tu peux l'adapter pour convenir à ton cas

A dispo sinon!

Je vais essayer cela !! Merci pour ta réponse Juice !

Alors je ne parviens pas directement avec ce que tu proposes. (l'erreur est : "Erreur de compilation : Type défini par l'utilisateur non défini") par rapport à :

Dim objFSO As Scripting.FileSystemObject

J'essaye d'adapter avec ce que j'utilisais !

bonjour

salut au passage Juice

je suggère Power BI Desktop gratuit

tu le télécharges, puis tu lui demandes de obtenir des données dans ton répertoire contentant TOUS tes fichiers

et tu vas rapidement savoir afficher des valeurs selon des dates (tu créeras une sorte de segment, ou filtre) de dates.

trèèèès puissant

amitiés à tous

Re bonjour à tous !

J'ai pu adapter un morceau du code de Juice dans le code que j'utilisais et ca fonctionne !

Merci beaucoup !!

strDossier = "\\Issou-servvlt\INDICATEURS_DE_PROD\"
strIndicECRnoeuds = Dir(strDossier & "\*06_NEW_ECR_WIP_V01.xls*", vbNormal)
    Do While strIndicECRnoeuds <> ""
          If Format(FileDateTime(strDossier & strIndicECRnoeuds), "dd/mm/yyyy") Like DTPicker1.Value & "*" Then
                      latestECRnoeuds = strDossier & strIndicECRnoeuds
            latestECRnoeuds = strDossier & strIndicECRnoeuds
        End If
        strIndicECRnoeuds = Dir
    Loop

If Not IsOpen(latestECRnoeuds) Then 'OUVERTURE INDICATEUR'
    Workbooks.Open latestECRnoeuds
Else
End If

'DEPUIS INDICATEUR
nomINDICECR = ActiveWorkbook.Name
Rechercher des sujets similaires à "vba rechercher ouvrir fichier suivant date"