Gå til hovedindhold

Hvordan gemmes, eksporteres flere / alle ark for at adskille csv- eller tekstfiler i Excel?

Når du bruger Excel, kan du manuelt gemme et regneark som en csv- eller tekstfil ved at bruge funktionen Gem som. Men hvordan kan du konvertere flere eller alle regneark i en projektmappe til adskilte csv- eller tekstfiler? I denne artikel vil vi vise dig metoder til at gemme eller konvertere flere eller alle ark til adskilte csv- eller tekstfiler.

Gem, eksporter eller konverter alle ark til csv eller tekstfil med VBA-kode
Gem, eksporter eller konverter flere / alle ark til csv- eller tekstfil med Kutools til Excel


Gem, eksporter eller konverter alle ark til csv eller tekstfil med VBA-kode

Med nedenstående VBA-koder kan du gemme alle regneark i en projektmappe til adskilte csv- eller tekstfiler. Gør som følger.

Eksporter eller konverter alle ark til csv-filer

1. Trykke andre + F11 taster samtidigt for at åbne Microsoft Visual Basic-applikation vindue.

2. i Microsoft Visual Basic-applikation vindue, skal du klikke på indsatte > Moduler. Kopier og indsæt derefter følgende kode i Moduler vindue.

VBA-kode: Eksporter alle ark til adskilte 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. Tryk på F5 nøgle for at køre koden. Du vil se alle eksporterede csv-filer findes på Dokumenter folder. Se skærmbillede:

Eksporter eller konverter alle ark til tekstfiler

Følgende kode kan hjælpe dig med at eksportere eller konvertere alle ark i projektmappen til adskilte tekstfiler.

VBA-kode: Eksporter alle ark til adskilte tekstfiler

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 eksporterede tekstfiler findes også på Dokumenter folder. Se skærmbillede:


Gem, eksporter eller konverter flere / alle ark til csv- eller tekstfil med Kutools til Excel

YDu kan hurtigt eksportere eller konvertere flere eller alle ark til individuelle csv-filer, tekstfiler eller xls-filer i Excel med Opdelt arbejdsbog nytte af Kutools til Excel.

Før påføring Kutools til Excel, Bedes download og installer det først.

1. klik Kutools Plus > Workbook > Opdelt arbejdsbog. Se skærmbillede:

2. i Opdelt arbejdsbog dialog boks:

  • 1). Hvis du vil konvertere alle ark til csv- eller tekstfiler, skal du bare holde alle arknavne markeret i Navn på regneark boks; Hvis du bare vil konvertere flere ark, skal du fortsætte med at kontrollere dem og fjerne markeringen fra andre ark, du ikke vil konvertere.
  • 2). I Indstillinger afsnit, tjek Filtype og vælg derefter Unicode-tekst (*. Txt) or CSV (Macintosh) (*. Csv) fra rullelisten.
  • 3). Klik på Dele .

3. i Gennemse for mappe i dialogboksen, skal du angive en mappe for at gemme de eksporterede filer og derefter klikke på OK .

Nu konverteres alle ark eller specificerede ark til adskilte csv- eller tekstfiler og finder i mappen som du angav ovenfor.

  Hvis du vil have en gratis prøveperiode (30 dage) af dette værktøj, klik for at downloade det, og gå derefter til at anvende handlingen i henhold til ovenstående trin.


Gem, eksporter eller konverter flere / alle ark til csv- eller tekstfil med Kutools til Excel

Bedste kontorproduktivitetsværktøjer

🤖 Kutools AI Aide: Revolutionér dataanalyse baseret på: Intelligent udførelse   |  Generer kode  |  Opret brugerdefinerede formler  |  Analyser data og generer diagrammer  |  Aktiver Kutools funktioner...
Populære funktioner: Find, fremhæv eller identificer dubletter   |  Slet tomme rækker   |  Kombiner kolonner eller celler uden at miste data   |   Runde uden formel ...
Super opslag: VLookup med flere kriterier    Multiple Value VLookup  |   VOpslag på tværs af flere ark   |   Fuzzy Lookup ....
Avanceret rulleliste: Opret hurtigt rulleliste   |  Afhængig rulleliste   |  Multivælg rulleliste ....
Column Manager: Tilføj et bestemt antal kolonner  |  Flyt kolonner  |  Skift synlighedsstatus for skjulte kolonner  |  Sammenlign områder og kolonner ...
Fremhævede funktioner: Grid fokus   |  Designvisning   |   Stor Formel Bar    Arbejdsbog & Ark Manager   |  Ressourcebibliotek (Autotekst)   |  Datovælger   |  Kombiner regneark   |  Krypter/Dekrypter celler    Send e-mails efter liste   |  Superfilter   |   Specielt filter (filter fed/kursiv/gennemstreget...) ...
Top 15 værktøjssæt12 tekst Værktøjer (tilføje tekst, Fjern tegn, ...)   |   50 + Chart Typer (Gantt kort, ...)   |   40+ Praktisk formler (Beregn alder baseret på fødselsdag, ...)   |   19 Indsættelse Værktøjer (Indsæt QR-kode, Indsæt billede fra sti, ...)   |   12 Konvertering Værktøjer (Tal til ord, Valutaomregning, ...)   |   7 Flet og del Værktøjer (Avancerede kombinere rækker, Opdel celler, ...)   |   ... og mere

Overlad dine Excel-færdigheder med Kutools til Excel, og oplev effektivitet som aldrig før. Kutools til Excel tilbyder over 300 avancerede funktioner for at øge produktiviteten og spare tid.  Klik her for at få den funktion, du har mest brug for...

Beskrivelse


Fanen Office bringer en grænseflade til et kontor med Office, og gør dit arbejde meget lettere

  • Aktiver redigering og læsning af faner i Word, Excel, PowerPoint, Publisher, Access, Visio og Project.
  • Åbn og opret flere dokumenter i nye faner i det samme vindue snarere end i nye vinduer.
  • Øger din produktivitet med 50 % og reducerer hundredvis af museklik for dig hver 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