Hoppa till huvudinnehåll

Hur exporterar jag Excel-data (urval eller ark) till textfiler i Excel?

Om du måste leverera en arbetsbok i textfilformat måste du konvertera arbetsboken till textfiler. Och den här artikeln presenterar dig två metoder för att enkelt exportera Excel-data till textfiler.

Exportera ett enda ark till textfil i Excel

Som standard sparas Excel-data som arbetsböcker i formatet .xlsx. Vi kan dock exportera ett kalkylblad med befintlig arbetsbok som textfiler med Spara som funktion. Gör så här:

1. Flytta till kalkylbladet som du exporterar till textfil och klicka på Fil (eller Kontorsknapp)> Spara som.

2. I öppningen Spara som i dialogrutan, välj den destinationsmapp som du vill spara den exporterade textfilen till, namnge filen i Filnamn och välj sedan Unicode Text (* .txt) från Spara som rullgardinsmeny. Se skärmdump:
exportera till textfil med Spara som-funktionen

3. Sedan kommer det att dyka upp två varningsdialogrutor som ber dig att bara exportera aktivt kalkylblad som textfil och utelämna alla inkompatibla funktioner med textfil. Klick OK knappen och Ja knappen successivt.

Sedan exporteras data i det aktiva kalkylbladet som en ny textfil.

Anmärkningar: den Spara som kommandot kan bara exportera data från det aktiva kalkylbladet som en textfil. Om du vill exportera all data i hela arbetsboken måste du spara varje kalkylblad som en textfil separat.


Exportera markering (eller en kolumn) till textfil med VBA

Följande VBA-kod kan också hjälpa dig att exportera vald data (till exempel en kolumn) till textfilen, gör så här:

1. Håll ner ALT + F11 knapparna och det öppnar Microsoft Visual Basic för applikationer fönster.

2. Klicka Insert > Modulernaoch klistra in följande kod i modulfönstret.

VBA: exportera urval eller hela kalkylbladet till textfilen

Sub ExportRangetoFile()
'Update 20130913
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

3. Tryck sedan på F5 nyckel för att köra den här koden. Välj sedan det dataintervall som du vill exportera i popup-dialogrutan och klicka på OK knapp, se skärmdump:
Välj en kolumn som du vill exportera som textfil

4. Och nu i en annan Spara som-dialogruta, ange ett namn för den här textfilen och en mapp för att placera den här filen och klicka på Save knapp.


Exportera markeringen (eller en kolumn) till textfilen med Kutools för Excel

Smakämnen Spara som kommandot kan exportera all data i det aktiva kalkylbladet som text. Vad händer om du exporterar all data från specificerade inaktiva kalkylblad eller delar av data i ett kalkylblad som en textfil? Kutools för Excel's Exportera intervall till fil verktyget kan hjälpa oss att exportera och spara data i valet som en textfil enkelt ..  

Kutools för Excel - Fullpackad med över 300 viktiga verktyg för Excel. Njut av en fullfjädrad 30-dagars GRATIS provperiod utan behov av kreditkort! Ladda ner nu!

1. Välj det ringde du ska exportera till en textfil (i vårt fall väljer vi kolumn A) och klickar på Kutools Plus > Import Export > Exportera intervall till fil, se skärmdump:
Excel-tillägg: exportera val till textfil

2. I Exportera intervall till fil dialogrutan, gör som nedan visas skärmdump:
Excel-tillägg: exportera val till textfil
(1) Kontrollera Unicode Text alternativet i filformat sektion;
(2) Markera alternativet Spara de aktuella värdena eller Spara värden som visas på skärmen som du behöver i avsnittet Textalternativ;
(3) Ange destinationsmapp som du ska spara den exporterade textfilen till;
(4) Klicka på Ok knapp.

3. Namnge den exporterade textfilen i den nya öppningsdialogrutan och klicka på Ok knapp.
namnge den exporterade textfilen

Och sedan har markeringen (vald kolumn A) exporterats som en textfil och sparats i den angivna mappen.

Kutools för Excel - Superladda Excel med över 300 viktiga verktyg. Njut av en fullfjädrad 30-dagars GRATIS provperiod utan behov av kreditkort! Hämta den nu


Exportera flera ark för att separera textfiler i Excel

Ibland kan du behöva exportera flera kalkylblad till flera textfiler i Excel. Spara som-funktionen kan vara lite tråkig! Oroa dig inte! Kutools för Excel Dela arbetsbok verktyget ger Excel-användare en enkel lösning för att exportera flera kalkylblad till många separata textfiler med flera klick. 

Kutools för Excel - Fullpackad med över 300 viktiga verktyg för Excel. Njut av en fullfjädrad 30-dagars GRATIS provperiod utan behov av kreditkort! Ladda ner nu!

1. klick Kutools Plus > ArbetsbokDela arbetsbok. Se skärmdump:
Excel-tillägg: exportera flera ark till textfiler

2. I dialogrutan Öppna delad arbetsbok gör du så här:
Excel-tillägg: exportera flera ark till textfiler
(1) Kontrollera kalkylblad som du exporterar till separata textfiler i Arbetsbokens namn sektion;
(2) Kontrollera Ange spara format alternativet och välj sedan Unicode-text (* .txt) nedifrån listrutan, se vänster skärmdump:
(3) Klicka på Split knapp.

3. Och sedan i dialogrutan Bläddra efter mapp som dyker upp väljer du destinationsmappen som du vill spara exporterade textfiler i och klickar på OK knapp.

Hittills har varje specificerat kalkylblad exporterats som en separat textfil och sparats i den angivna mappen.

Kutools för Excel - Superladda Excel med över 300 viktiga verktyg. Njut av en fullfjädrad 30-dagars GRATIS provperiod utan behov av kreditkort! Hämta den nu

Massspara enkelt varje ark enkelt som en separat PDF / text / csv-fil eller arbetsbok i Excel

Normalt kan vi spara aktivt kalkylblad som en separat .pdf-fil, .txt-fil eller .csv-fil med funktionen Spara som. Men Kutools för Excel Dela arbetsbok verktyget kan hjälpa dig att enkelt spara varje arbetsbok som separat PDF / TEXT / CSV-fil eller arbetsbok i Excel.


ad split arbetsbok pdf 1

Kutools för Excel - Superladda Excel med över 300 viktiga verktyg. Njut av en fullfjädrad 30-dagars GRATIS provperiod utan behov av kreditkort! Hämta den nu


Demo: exportera Excel-data (urval eller ark) till textfiler i Excel


Kutools för Excel: Över 300 praktiska verktyg till hands! Börja din 30-dagars gratis provperiod utan funktionsbegränsningar idag. Ladda ner nu!

Relaterade artiklar:

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 (19)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How would I amend the code for Export selection (or one column) to text file with VBA to autofill the filename with the contents of cell B2?
thanks
This comment was minimized by the moderator on the site
Hi,This is great, thanks a lot.
How would I alter the code for Export selection (or one column) to text file with VBA to auto fill the filename with the contents of cell B2?
This comment was minimized by the moderator on the site
Hi, this code is very useful for me. So thank you very much.
I want to ask that: When i'm using this code, a new line append automatically at the end of the txt content.
Can you help me about preventing this by vba?
This comment was minimized by the moderator on the site
hi guys, i used that code:
Sub ExportRangetoFile()
'Update 20130913
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

and have working, but i need to savea column with cells that contain the function"concatenate", and when i play the vba code, and i save the .TXT file, only what appears it's #REF. how can i to fix it? cause i need the data that appears on that cells?
This comment was minimized by the moderator on the site
Has anyone been able to figure this out? I am having the same issue.
This comment was minimized by the moderator on the site
HELLO
THANK YOU FOR YOUR VBA CODE
SOMETIMES ON CERTAIN TEXT THERE IS "TEXT" IN EXPORT .TXT
THANK YOU FOR WHY
This comment was minimized by the moderator on the site
Thanks for the awesome piece of VBA code to export data to a text file. I have used your code with some of my own. The data I am dealing with is extremely line-length specific and after the macro has run, the text file contains some double quotation marks " at random places, which was never present in my data before. I have tried adding a code line to remove this character at various stages but that does not seem to be working. I am aware that I can manually remove this but would like to resolve it in the VBA code.

Sub Macro3()
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$10591").AutoFilter Field:=1, Criteria1:= _
"=NSZAP*", Operator:=xlAnd
Range("A1").Select
ActiveCell.Offset(200, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Rows("1:10101").EntireRow.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Selection.AutoFilter
Range(Selection, Selection.End(xlDown)).Select
Set WorkRng = Application.Selection
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
Selection.Replace What:="""", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
Selection.Replace What:="""", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
Selection.Replace What:="""", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Hi Harish,
For removing a specified character in bulk, you can replace the character with nothing.
Ctrl + H to open the Find and Replace dialog box, type the specified character in the Find what box, and type noting in the replace with box.
This comment was minimized by the moderator on the site
3/2 You might also note that you don't need to specify the wb prefix once you've .Add ed the workbook since it becomes active. Specifying wb might or might not be more efficient but it can be omitted from some commands. Actually I entirely scrapped the variable wb; I just go Workbooks.Add, and use ActiveWorkbook when needed. (As you are suppressing ScreenUpdating it wouldn't be obvious to some that wb is Active. Tip for beginners (and higher): I always, always develop with ScreenUpdating and DisplayAlerts as True, and when done with development, I consider toggling them off for some passage of code.)
This comment was minimized by the moderator on the site
2/2 - vbYesNoCancel msgboxes and Booleans to indicate whether to export all, selection, or a user specified range - a static String for the prior range address - if len(that static)1 then I set WorkRng=activesheet.UsedRange (You can't copy multiple areas with a single copy, though with a little work you could walk the areas and copy them piecemeal.) - Idiotic Microsoft does not save off empty rows at the start and the bottom of the saved range, and does not save off empty columns at the leftmost and the rightmost of the saved range. When I detect that (UsedRange is not row 1, col. 1, or xlLastCell is not completely lower-right) I msgbox to ask user if they want to plug A1 or the lower right cell. - Then I decided to preemptively address the upper left issue by inserting a row and setting A1 to be text like "The following is for range " - I close with activeworkbook.close SaveChanges:=False - Long time habit since I'm an angry proponent of cleaning up (and the world is a sad place thanks to irresponsible programmers who shirk that responsibility when it matters), I set WorkRng = Nothing on the way out :)
This comment was minimized by the moderator on the site
1/2 (since your software limits comment length) I am a serious coder and I want to inform you that the VBA code here is outstanding. It's clean, and not one line too much, or one too few. It's exemplary coding for demonstration of the process. I'll also mention that as I extensively researched solutions for export of selection, you and only one other person suggested dropping the range into a temp new workbook. All other answers were painfully manually, walking through cell by cell. Even Chip's code is much longer and runs slower (but intentionally so, as it is much more flexible - specifiable delimiters, etc.) Just for your amusement, I made some very minor tweaks but otherwise nearly lifted the code word for word because it basically dropped right into a very intricate and specialized application. Some of these tweaks are something I'm sure you do in practice, but they add lines of code (e.g. error checking) so showing them on this webpage would have muddied your display so that the essential elements of processing would have been cumbersome for readers to follow along. Anyway: (see part 2)
This comment was minimized by the moderator on the site
My query is as mentioned below: I have a report to generate everyday. The data what I get on the final row (only one row but more than one columns) I want it to export to another excel sheet which is a summary excel sheet saved separately on my desktop. Eg: Day1 report - final row export to Summary excel sheet row1 Day2 report - final row export to Summary excel sheet row2 Day3 report - final row export to Summary excel sheet row3 and so on.. Export should be done through a click button., which means when I click on export button the data on the sheet I calculated today should go and save on the Summary excel sheet row1, next day a new excel sheet report calculated should go and save on the Summary excel sheet row2 and so on.. Will this be possible to do... If yes please someone help me on this... Thank you in advance...:-)
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