Hoppa till huvudinnehåll

Hur sparar jag, exporterar flera / alla ark till separata csv- eller textfiler i Excel?

När du använder Excel kan du manuellt spara ett kalkylblad som en csv- eller textfil genom att använda funktionen Spara som. Men för att konvertera flera eller alla kalkylblad i en arbetsbok till separerade csv- eller textfiler, hur kan du göra? I den här artikeln kommer vi att visa dig metoder för att spara eller konvertera flera eller alla ark till separerade csv- eller textfiler.

Spara, exportera eller konvertera alla ark till csv- eller textfil med VBA-kod
Spara, exportera eller konvertera flera / alla ark till csv eller textfil med Kutools för Excel


Spara, exportera eller konvertera alla ark till csv- eller textfil med VBA-kod

Med nedanstående VBA-koder kan du spara alla kalkylblad i en arbetsbok till separata csv- eller textfiler. Gör så här.

Exportera eller konvertera alla ark till csv-filer

1. Tryck andra + F11 samtidigt för att öppna Microsoft Visual Basic-applikation fönster.

2. I Microsoft Visual Basic-applikation fönstret klickar Insert > Modulerna. Kopiera och klistra sedan in följande kod i Modulerna fönster.

VBA-kod: Exportera alla ark till separata csv-filer

Sub ExportSheetsToCSV()
	Dim xWs As Worksheet
	Dim xcsvFile As String
	For Each xWs In Application.ActiveWorkbook.Worksheets
		xWs.Copy
		xcsvFile = CurDir & "\" & xWs.Name & ".csv"
		Application.ActiveWorkbook.SaveAs Filename: = xcsvFile, _
		FileFormat: = xlCSV, CreateBackup: = False
		Application.ActiveWorkbook.Saved = True
		Application.ActiveWorkbook.Close
	Next
End Sub

3. tryck på F5 för att köra koden. Du ser att alla exporterade csv-filer finns på Dokument mapp. Se skärmdump:

Exportera eller konvertera alla ark till textfiler

Följande kod kan hjälpa dig att exportera eller konvertera alla ark i arbetsboken till separata textfiler.

VBA-kod: Exportera alla ark till separata textfiler

Sub ExportSheetsToText()
	Dim xWs As Worksheet
	Dim xTextFile As String
	For Each xWs In Application.ActiveWorkbook.Worksheets
		xWs.Copy
		xTextFile = CurDir & "\" & xWs.Name & ".txt"
		Application.ActiveWorkbook.SaveAs Filename: = xTextFile, FileFormat: = xlText
		Application.ActiveWorkbook.Saved = True
		Application.ActiveWorkbook.Close
	Next
End Sub

De exporterade textfilerna finns också på Dokument mapp. Se skärmdump:


Spara, exportera eller konvertera flera / alla ark till csv eller textfil med Kutools för Excel

YDu kan snabbt exportera eller konvertera flera eller alla ark till enskilda csv-filer, textfiler eller xls-filer i Excel med Dela arbetsbok nytta av Kutools för Excel.

Innan du ansöker Kutools för ExcelBer ladda ner och installera det först.

1. klick Kutools Plus > Arbetsbok > Dela arbetsbok. Se skärmdump:

2. I Dela arbetsbok dialog ruta:

  • 1). Om du vill konvertera alla ark till csv- eller textfiler, behåll bara alla arknamn markerade i Kalkylbladets namn låda; Om du bara vill konvertera flera ark fortsätter du att kontrollera dem och avmarkera andra ark som du inte vill konvertera.
  • 2). I Tillbehör avsnitt, kolla på Spara som och välj sedan Unicode-text (*. Txt) or CSV (Macintosh) (*. Csv) från rullgardinsmenyn.
  • 3). Klicka på Split knapp.

3. I Bläddra för mapp i dialogrutan, ange en mapp för att spara de exporterade filerna och klicka sedan på OK knapp.

Nu konverteras alla ark eller angivna ark till separata csv- eller textfiler och lokaliseras i mappen som du angav ovan.

  Om du vill ha en gratis provperiod (30 dagar) av det här verktyget, klicka för att ladda ner den, och gå sedan till för att tillämpa operationen enligt ovanstående steg.


Spara, exportera eller konvertera flera / alla ark till csv eller textfil med Kutools för Excel

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
Comments (38)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, thanks for the VBA, i have a problem with the VBA for export to text file, as the VBA run the txt file got generated, but the inside data is not exactly like the excel data, for example my data include character quotation mark, and the exported data became double quotation mark, plus the beginning and the ending of the text file there are quotation mark, here's the excel and the txt file that i meant :
excel : Start Chrome https://xxx/tap_inbox_sparepart_act.asp?proses_id=202300102027800&act=akseptoraksep"&"alasan=ok
text file : "Start Chrome https://xxx/tap_inbox_sparepart_act.asp?proses_id=202300102027800&act=akseptoraksep""&""alasan=ok"

Thanks in advanced :)
This comment was minimized by the moderator on the site
Hi Nicky,

I tested this code, but could not reproduce the problem in my case. And the file you provided didn't open. Can you upload your file using the "upload attachment" link below?
This comment was minimized by the moderator on the site
The VBA code works nicely. Equivalent C# code in a VSTO workbook project emits empty .csv files.


void SaveSomeWorksheetsAsCsvFiles() {
    string[] worksheetNames = new[] { "Sheet1", "Sheet2", "Sheet3" };
    foreach (var worksheet in worksheetNames.Select(_ => Globals.ThisWorkbook.Sheets[_] as Excel.Worksheet)) {
        worksheet.Copy();
        string fullName = @"C:\Users\....\Staging" + $".{worksheet.Name}.csv";
        this.SaveAs(fullName, XlFileFormat.xlCSV);
        Application.ActiveWorkbook.Saved = true;
        Application.ActiveWorkbook.Close();
    }
}
This comment was minimized by the moderator on the site
Hi Calvin,
We do not support programming languages other than VBA. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Hi, thanks for the great code (Export all sheets to separated Text files), using it a lot. Just ran into a workbook where it won't work, debugger says line xWs.Copy is problem, popup says method copy of a workbook failed. Is there any restrictions concerning a worksheet name or something similar, like no merged cells etc..?

Thanks for a reply :)
This comment was minimized by the moderator on the site
HI BI,
Does your worksheet contain a pivot table? Can you provide us with your data for tesing? If you don't mind, upload your sample file here.
This comment was minimized by the moderator on the site
Hi, thanks for the great code it is awesome(Export all sheets to separated Text files)! Used it on many occasions but ran into a file where it won't work, debugger says runtime error 1004 and that method copy of a worksheet failed and marks line xWs.Copy. Is there any rule concerning a worksheet name that would not allow code to run?
This comment was minimized by the moderator on the site
Thanks! I used this to save all the sheets of my .xlsx file into differents .xlsx
Sub ExportSheetsToXLSX()
Dim xWs As Worksheet
Dim xcsvFile As String
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Copy
xlsxFile = CurDir & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.SaveAs Filename:=xlsxFile, _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.ActiveWorkbook.Saved = True
Application.ActiveWorkbook.Close
Next
End Sub
This comment was minimized by the moderator on the site
how would I change the code to have it save in a different file format such as an xlsx? or ASCII.
This comment was minimized by the moderator on the site
So, this is great! BUT, what if I need to split a workbook into .csv but for PC, not MAC? If anyone has a way of doing it, please help me out. I will be very grateful.
This comment was minimized by the moderator on the site
Hi,
The first VBA code in this post can do you a favor.
This comment was minimized by the moderator on the site
Is there a way to use this code to export only a certain range to a .csv file of each sheet in a book?
This comment was minimized by the moderator on the site
Hi,
The VBA code in this article may do you a favor: https://www.extendoffice.com/documents/excel/2897-excel-export-range-to-csv.html
This comment was minimized by the moderator on the site
Hello,
thank you so much for a great macro, it works like a charm! But I have a question, what if I would like to save this macro (CSV version) on PERSONAL.xlsb as to make it available on any excel instance?

When I try, CurDir takes the PERSONAL directory, instead of the active worksheet one...

Thank you for any help!
This comment was minimized by the moderator on the site
Hi Mirko,
Method in this article can do you a favor: https://trumpexcel.com/personal-macro-workbook/.
This comment was minimized by the moderator on the site
If you just want to convert multiple sheets, keep checking them and going to uncheck other sheets you don’t want to convert
sir for thuis step do you have macro
This comment was minimized by the moderator on the site
Good day,
Can't help with VBA code for this. Why not try the Split Workbook utility we provide in the post? It will save your time and you will love it.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations