Fermer plusieurs classeurs à la fin d'un traitement

Fermé
VBA_93 - Modifié par VBA_93 le 21/12/2016 à 17:05
 VBA_93 - 21 déc. 2016 à 17:23
Bonjour à tous,

J'ai actuellement une macro qui me permet de réaliser une conversion de fichier , la voici :

Sub ConvertDBF_to_CSV()
Dim strDocPath As String
Dim strCurrentFile As String
Dim Fname As String
Dim sFiles
Dim x As Integer, y As Integer
Application.ScreenUpdating = True

x = 0
y = 0
sFiles = Dir(ThisWorkbook.Path & "*.dbf")
'count the files
Do Until sFiles = ""
x = x + 1
sFiles = Dir
Loop

strDocPath = "C:\Users\fg733136\Documents\Projet PCC\03SABRAQUE corrigé\03SABRAQUE corrigé\"
'strCurrentFile = Dir(strDocPath & "*.*")
strCurrentFile = Dir(strDocPath & "*.dbf")

Do While strCurrentFile <> ""
y = y + 1
'display current status on status bar
Application.DisplayStatusBar = True

Application.StatusBar = "Conversion " & y & " of " & x

Workbooks.Open Filename:=strDocPath & strCurrentFile
Fname = Left$(strCurrentFile, Len(strCurrentFile) - 4) & ".csv"
ActiveWorkbook.SaveAs Filename:=strDocPath & Fname, FileFormat:=6, CreateBackup:=False, local:=True

strCurrentFile = Dir
Loop
Application.StatusBar = False 'release the status bar back to excel
Application.ScreenUpdating = False
End Sub

Elle convertit les fichiers dans le format voulu mais résultat nous ouvre pour chaque fichier converti un classeur. J'aimerais savoir si avec cette macro :
Sub CloseWorbook(WordbookName As String)
Dim Wbk As Excel.Workbook
For Each Wbk In Excel.Workbooks
If Wbk.Name = WordbookName Then
Wbk.Close
End If
Next
End Sub

J'étais capable de fermer tous les fichiers converti par ma macro précédente.
Merci d'avance pour vos réponses.

1 réponse

Voila ce que j'ajouterais après ma sauvegarde soit mon ActiveWorkbook.SaveAs :
For Each Wbk In Excel.Workbooks
If Wbk.Name = WorkbookName Then
Wbk.Close
End If
0
Voila la macro complète selon moi :

Sub ConvertDBF_to_CSV()
Dim strDocPath As String
Dim strCurrentFile As String
Dim Fname As String
Dim sFiles
Dim x As Integer, y As Integer
Dim WorkbookName As String
Dim Wbk As Excel.Workbook
WorkbookName = strDocPath & Fname
Application.ScreenUpdating = True

x = 0
y = 0
sFiles = Dir(ThisWorkbook.Path & "*.dbf")
'count the files
Do Until sFiles = ""
x = x + 1
sFiles = Dir
Loop

strDocPath = "C:\Users\fg733136\Documents\Projet PCC\03SABRAQUE corrigé\03SABRAQUE corrigé\"
'strCurrentFile = Dir(strDocPath & "*.*")
strCurrentFile = Dir(strDocPath & "*.dbf")

Do While strCurrentFile <> ""
y = y + 1
'display current status on status bar
Application.DisplayStatusBar = True

Application.StatusBar = "Conversion " & y & " of " & x

Workbooks.Open Filename:=strDocPath & strCurrentFile
Fname = Left$(strCurrentFile, Len(strCurrentFile) - 4) & ".csv"
ActiveWorkbook.SaveAs Filename:=strDocPath & Fname, FileFormat:=6, CreateBackup:=False, local:=True
For Each Wbk In Excel.Workbooks
If Wbk.Name = WorkbookName Then
Wbk.Close
End If
Next
strCurrentFile = Dir
Loop
Application.StatusBar = False 'release the status bar back to excel
Application.ScreenUpdating = False
End Sub
0