- To post as a guest, your comment is unpublished.· 2 months agoSo, after reviewing the code a little closer, I saw where the initial file type had to be .xls. So replacing with .csv has solved the issue.
Come convertire in batch più file Excel in file CSV in Excel?
In Excel, possiamo convertire la cartella di lavoro in file CSV con la funzione Salva con nome, ma potresti sapere come convertire in batch più file Excel in file CSV separati? In questo articolo, introduco un codice VBA per convertire in batch tutti i file Excel in una cartella in file CSV in Excel.
In Excel, non esiste alcuna funzione incorporata in grado di risolvere rapidamente questo lavoro tranne VBA.
1. Abilitare Excel e premere Alt + F11 chiavi aperte Microsoft Visual Basic, Applications Edition finestra.
2. Clic inserire > Modulo per creare un nuovo modulo.
3. Copia il codice sottostante e incollalo nella nuova finestra del modulo.
VBA: converte in batch i file Excel in CSV
Sub WorkbooksSaveAsCsvToFolder() 'UpdatebyExtendoffice20181031 Dim xObjWB As Workbook Dim xObjWS As Worksheet Dim xStrEFPath As String Dim xStrEFFile As String Dim xObjFD As FileDialog Dim xObjSFD As FileDialog Dim xStrSPath As String Dim xStrCSVFName As String Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual On Error Resume Next Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker) xObjFD.AllowMultiSelect = False xObjFD.Title = "Kutools for Excel - Select a folder which contains Excel files" If xObjFD.Show <> -1 Then Exit Sub xStrEFPath = xObjFD.SelectedItems(1) & "\" Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker) xObjSFD.AllowMultiSelect = False xObjSFD.Title = "Kutools for Excel - Select a folder to locate CSV files" If xObjSFD.Show <> -1 Then Exit Sub xStrSPath = xObjSFD.SelectedItems(1) & "\" xStrEFFile = Dir(xStrEFPath & "*.xls*") Do While xStrEFFile <> "" Set xObjWB = Workbooks.Open(Filename:=xStrEFPath & xStrEFFile) xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv" xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV xObjWB.Close savechanges:=False xStrEFFile = Dir Loop Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True Application.ScreenUpdating = True End Sub
4. Stampa F5 chiave, seleziona la cartella contenente i file Excel che desideri convertire in file CSV nella prima finestra di dialogo.
5. Clic OK, quindi nella seconda finestra di dialogo popping, seleziona la cartella in cui posizionare i file CSV.
6. Clic OK, ora i file Excel nella cartella sono stati convertiti in file CSV e salvati in un'altra cartella.
Come sappiamo, possiamo solo convertire l'intera cartella di lavoro in un file CSV in Excel con la sua funzione Salva con nome. Ma a volte, vuoi convertire il singolo foglio in file CSV, in questo caso, il Cartella di lavoro divisa utilità di Kutools for Excel posso aiutarti.
|Kutools for Excel, con oltre 300 funzioni utili, rende il tuo lavoro più facile.|
Dopo l'installazione Kutools per Excel, per favore fai come di seguito:（Scarica subito Kutools per Excel!)
2. Nel Cartella di lavoro divisa finestra di dialogo, seleziona il nome del foglio che desideri dividere (tutti i fogli sono selezionati per impostazione predefinita), seleziona Salva con nome digita, scegli CSV (Macintosh) (* .CSV) dall'elenco a discesa.
You are guest
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 2 months agoHello, is there a quick change to the code that would allow me to change from a CSV UTF-8 (Comma delimited) to just CSV (Comma delimited)? I tried the first method and was hopeful but it seems like it won't change them since they are already in some form of CSV. Maybe there is an easier process but I can't find anything. I have to convert maybe 150 files that were saved in this format and I don't want to open every file and Save As if I can avoid it. Any help is appreciated!
- To post as a guest, your comment is unpublished.· 3 months agoThis is amazing. Thank-you!
- To post as a guest, your comment is unpublished.· 4 months agotop thanks :)
- To post as a guest, your comment is unpublished.· 1 years agoI think it is worth adding better error handling for files with special characters, currently they are simply ignored.
- To post as a guest, your comment is unpublished.· 1 years agoThank you for sharing. I'm trying to save out multiple xls files which contain a unique value, producing a prompt asking yes or no before saving. The prompt reads..
"Some features in your workbook might be lost if you save it as a CSV (Comma delimited). Do you want to keep using that format?"
Would someone know where to add the code to answer yes to this prompt?
- To post as a guest, your comment is unpublished.· 2 years agoAnother small remark:
If the cells in the original Excel files are all formatted as "General", some accuracy is lost when the file is saved as a CSV
For example, if a cell value in Excel is 0.123456789123456, then the value in the CSV will be 0.123456789 (missing the remaining decimals), as long as the cell was formatted as 'General'. This can be solved by formatting all cells in the Excel file to anything else than 'General' (for example, 'Text'). In that case, the CSV *will* still have the full detail/accuracy. I.e. the values in the Excel files will be fully intact after saving as a CSV.
How could this macro be changes, so it sets the formatting of all cells in the Excel file to 'Text', before saving as a CSV?
I imagine that it must somehow make use of the following, but I can't figure out how to correctly include in within the macro:
Selection.NumberFormat = "@"
- To post as a guest, your comment is unpublished.· 2 years agoWorks great, thanks for the code!
My only remark would be that this code cuts of file names when there is a "." in the filename itself (e.g. file.123.csv turns into file.csv).
- To post as a guest, your comment is unpublished.· 1 years agoHave you found a way around this issue?
- To post as a guest, your comment is unpublished.· 1 years agoCarol,
On line 33 I've replaced this code:
xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv"
With this code:
xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".xlsx") - 1) & ".csv"
Note that if you're using some other excel extension (.xls, .xlsm, etc.) you should change it as such :)
- To post as a guest, your comment is unpublished.· 1 years agoThank you so much! This has saved me so much time!!
- To post as a guest, your comment is unpublished.· 2 years agoTy it really works dear !!