Je suis en train de créer un bouton dans un. xlsm
qui va convertir chacun des ~ .xlsx
fichiers dans le myFolder
en .txt
. Le code VBA
ci-dessous renvoie une erreur Expected End Sub
. Les données sont toujours dans la 'Feuille 1' même si d'autres feuilles peuvent être présentesconvertir tous les fichiers xlsx dans le répertoire en texte
La commande Dos
exécute et convertit les fichiers mais ils sont illisibles (quelque chose à voir avec le formatage excel?) Je ne sais pas quoi faire? Merci :)
Dos
cd C:\Users\Desktop\folder
Copy *.xlsx *.txt
VBA
Option Explicit
Private Sub CommandButton1_Click()
Dim oFSO, myFolder
Dim xlText
myFolder = "C:\Users\Desktop\folder"
Set oFSO = CreateObject("Scripting.FileSystemObject")
xlText = -4158 'Excel txt format enum
Call ConvertAllExcelFiles(myFolder)
Set oFSO = Nothing
Call MsgBox("Done!")
Sub ConvertAllExcelFiles(ByVal oFolder)
Dim targetF, oFileList, oFile
Dim oExcel, oWB, oWSH
Set oExcel = CreateObject("Excel.Application")
oExcel.DisplayAlerts = False
Set targetF = oFSO.GetFolder(oFolder)
Set oFileList = targetF.Files
For Each oFile In oFileList
If (Right(oFile.Name, 4) = "xlsx") Then
Set oWB = oExcel.Workbooks.Open(oFile.Path)
For Each oWSH In oWB.Sheets
Call oWSH.SaveAs(oFile.Path & ".txt", FileFormat:=xlTextWindows)
Next
Set oWSH = Nothing
Call oWB.Close
Set oWB = Nothing
End If
Next
Call oExcel.Quit
Set oExcel = Nothing
End Sub
Merci beaucoup :). – Chris
Je suis content que cela ait aidé. J'ai fait de petits changements et amélioré les noms de fichiers texte: la version initiale a enregistré le fichier sous le nom de 'Book1.xlsx.txt' (maintenant il l'enregistre en tant que' Book1.txt') –