1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| Public Chemin, Fich As String, ReponseMsgBox As Variant
.
'routine d'appel depuis le bouton sur feuille
' .
Public Sub SelectionnerRepertoire()
Chemin = FLoadNomDuREP: Chemin = Trim(Chemin): If Chemin = "" Then Exit Sub
If Right(Chemin, 1) <> "\" Then Chemin = Chemin & "\"
DoEvents
'demande de confirmation
M$ = "Traiter tous les Fichiers xls du répertoire suivant :" & vbLf & Chemin & vbLf & vbLf & "Veuillez confirmer ?"
ReponseMsgBox = MsgBox(M$, vbQuestion + vbYesNo, "Traitement des fichiers")
If ReponseMsgBox = vbYes Then
BoucleDeTraitement ' appel la routine de traitement des fichiers
MsgBox "Traitement terminé !", vbInformation
Else
MsgBox "Traitement abandonné !", vbExclamation
End If
End Sub
' , &H1&)=avec bouton "créer un nouveau dossier" ... , $H201&)=sans le bouton
'objShell.BrowseForFolder(&H0&, "Sélectionnez un dossier", &H201&, RepDefaut)
Private Function FLoadNomDuREP() As String
Dim objShell As Object, objFolder As Object, REP As String
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(&H0&, "Sélectionnez un dossier", &H201&)
If Not objFolder Is Nothing Then
REP = objFolder.Items.Item.Path
If Right(REP, 1) <> "\" Then REP = REP & "\"
End If
FLoadNomDuREP = REP
Set objShell = Nothing: Set objFolder = Nothing
End Function
' .
Private Sub BoucleDeTraitement() ' la boucle de traitement des fichiers
Application.ScreenUpdating = False
ChDir Chemin
Fich = Dir(Chemin & "*.xls")
Do While Fich <> "" ' On effectue la boucle tant qu'il y a un fichier à traiter
Workbooks.Open Chemin & Fich
auto_open ' On appel notre macro auto_open
ActiveWorkbook.Close SaveChanges:=True ' On ferme le fichier et on sauvegarde les modifications
Fich = Dir
Loop
Application.ScreenUpdating = True
End Sub
Public Sub auto_open()
Dim resultat As String
resultat = InputBox("Valeur contenue dans cellules à nettoyer", "Nettoyage de cellules")
If resultat <> "" Then
Application.ScreenUpdating = False
Cells.SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants).Replace What:=resultat, Replacement:="", LookAt:=xlWhole, MatchCase:=True
Application.ScreenUpdating = True
End If
End Sub |
Partager