[VBA] Workbook pas ouvert, erreur 9 non détectée
Bonjour à tous,
J'ai un souci VBA et j'espérais qu'on puisse m'aider.
Ce que j'aimerais faire :
J'aimerais que ma macro active un autre classeur Excel depuis le classeur actif. Sauf que si le classeur n'est pas ouvert, elle doit permettre à l'utilisateur d'aller chercher le classeur à ouvrir. Jusque là, ça va. Le classeur à aller chercher doit avoir un nom qui commence par une suite spéficique : DBRD-G 20 (la suite peut varier).
Comment je m'y suis prise:
J'ai voulu créer un code qui dit que pour chaque classeur ouvert, si le nom commence par "DBRD-G 20" alors tu l'actives (il y en a toujours qu'un seul ouvert qui répond à cette condition, impossible d'en avoir plusieurs). Et si tu trouves un erreur de type 9, alors tu permets à l'utilisateur d'aller chercher le classeur.
Mon problème:
L'erreur de type 9 n'apparaît pas lorsqu'aucun classeur porte un nom qui commence par "DBRD-G 20".
Mon code
Dim strFileToOpen As String
On Error Resume Next
For Each Workbook In Application.Workbooks
If Left(Workbook.Name, 9) = "DBRD-G 20" Then
Workbook.Activate
Exit For
End If
If Err.Number = 9 Then
On Error GoTo 0
'Open a window in which the user must select the DBRD-G 2015_CURRENT file
strFileToOpen = Application.GetOpenFilename _
(Title:="Please open the DBRD-G file containing the Hit List", _
FileFilter:="Excel Files *.xlsx* (*.xlsx*),")
'If no file is selected, then write a message box and exit the process
If strFileToOpen = "" Then
MsgBox "No selected file.", vbExclamation, "Sorry!"
Exit Sub
'If the selected file does not contain "DBRD-G 20", then write a message box and exit the process
ElseIf Not UCase(strFileToOpen) Like "*DBRD-G 20*" Then
MsgBox "The selected file is not the DBRD-G file containing the Hit List", vbExclamation, "Sorry!"
Exit Sub
'If the user selected a file containing "DBRD-G 2015_CURRENT", then open it.
Else: Workbooks.Open Filename:=strFileToOpen
End If
End If
Next Workbook
Remarque:
Quand je change le début du code pour mettre directement le nom exacte du fichier, ça fonctionne!
On Error Resume Next
Workbooks("DBRD-G 2015_JKL").Activate
If Err.Number <> 0 Then
On Error GoTo 0
Une idée pour me débloquer ?
Merci
bonjour,
une proposition de correction
Dim strFileToOpen As String
On Error GoTo traiterreur
For Each Workbook In Application.Workbooks
If Left(Workbook.Name, 9) = "DBRD-G 20" Then
Workbook.Activate
Exit For
End If
ici:
If notopen Then
On Error GoTo 0
'Open a window in which the user must select the DBRD-G 2015_CURRENT file
strFileToOpen = Application.GetOpenFilename _
(Title:="Please open the DBRD-G file containing the Hit List", _
FileFilter:="Excel Files *.xlsx* (*.xlsx*),")
'If no file is selected, then write a message box and exit the process
If strFileToOpen = "" Then
MsgBox "No selected file.", vbExclamation, "Sorry!"
Exit Sub
'If the selected file does not contain "DBRD-G 20", then write a message box and exit the process
ElseIf Not UCase(strFileToOpen) Like "*DBRD-G 20*" Then
MsgBox "The selected file is not the DBRD-G file containing the Hit List", vbExclamation, "Sorry!"
Exit Sub
'If the user selected a file containing "DBRD-G 2015_CURRENT", then open it.
Else: Workbooks.Open Filename:=strFileToOpen
End If
End If
Next Workbook
'....
Exit Sub
traiterreur:
If Err.Number = 9 Then notopen = True: Resume ici
End Sub
Merci de m'avoir répondu, mais ça ne fonctionne pas. Quand j'enclenche la macro, rien ne se passe. :/ (j'ai juste remplacé mon code par le tien).
Est-ce que l'erreur pourrait se trouver dans cette partie ?
For Each Workbook In Application.Workbooks
If Left(Workbook.Name, 9) = "DBRD-G 20" Then
Workbook.Activate
Exit For
End If
Parce que ça remplace le nom complet, et ça fonctionne parfaitement avec le nom complet justement...
Bonjour,
à tester
Sub test1()
Dim strFileToOpen As String
For Each Workbook In Application.Workbooks
If Left(Workbook.Name, 9) = "DBRD-G 20" Then
Workbook.Activate
Exit For
End If
Next Workbook
If Left(ActiveWorkbook.Name, 9) <> "DBRD-G 20" Then
'Open a window in which the user must select the DBRD-G 2015_CURRENT file
strFileToOpen = Application.GetOpenFilename _
(Title:="Please open the DBRD-G file containing the Hit List", _
FileFilter:="Excel Files *.xlsx* (*.xlsx*),")
'If no file is selected, then write a message box and exit the process
If strFileToOpen = "" Then
MsgBox "No selected file.", vbExclamation, "Sorry!"
Exit Sub
'If the selected file does not contain "DBRD-G 20", then write a message box and exit the process
ElseIf Not UCase(strFileToOpen) Like "*DBRD-G 20*" Then
MsgBox "The selected file is not the DBRD-G file containing the Hit List", vbExclamation, "Sorry!"
Exit Sub
'If the user selected a file containing "DBRD-G 2015_CURRENT", then open it.
Else: Workbooks.Open Filename:=strFileToOpen
End If
End If
End Sub
Waaahouuu génial ça fonctionne du tonnerre ! C'est si simple pourtant
Très bonne journée !