I try to open a workbook in a folder which a user decide by entering Dateconso. Open the workbook copy/paste in another workbook.
The problem is that the code breaks at opening the file.
Execution error 1004:'11400. 16.01.xlsm' not found.
Here the thing the name of the workbook is 11400.16.01.xlsm without any space between 11400. and 16 (which is Dateconso).
I understand that it cannot open this workbook because it doesn't exist...but this is not the workbook I want to open !!!
Sub consolidation()
'
' Macro
' Déclaration des variables
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim Dateconso As String
'Optimisation de la Macro Speed
Application.ScreenUpdating = True
Application.EnableEvents = True
'Sélection de la date
Dateconso = InputBox("Quelle date souhaitez-vous consolider?","Question")
If Dateconso = "" Then Exit Sub 'Si rien exit le programme
'Trouve les fichiers qui on la date associée
myFile = Dir("Z:\7. Personnel\Florian\Projet_BDC\Test\*.xlsm")
While myFile <> ""
If InStr(myFile, Dateconso) > 0 Then 'si tu trouve la date recherchée, alors ouvre le fichier puis copie toute puis colle
Set wb = Workbooks.Open(Filename:=myFile)
wb.Worksheets(1).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("Consolidation.xlsm").Worksheets(2).Activate
ActiveSheet.Paste
wb.Close
Else: MsgBox "Fichiers introuvables, vérifiez le format de date entré" 'Si il ne trouve rien, préviens l'utilisateur
Exit Sub
End If
myFile = Dir()
Wend
End Sub
InputBoxwas type with the extra space in'11400. 16.01.xlsm? You might also want for direct equality by usingIf myFile = Dateconso, instead of usingInstr. This will rid you of simple manual type pages. - Scott Holtzman