Note: The other languages of the website are Google-translated. Back to English
Vpiši se  \/ 
x
or
x
Registracija  \/ 
x

or

Kako paketno pretvoriti več datotek Excel v datoteke CSV v Excelu?

V Excelu lahko delovni zvezek pretvorimo v datoteko CSV s funkcijo Shrani kot, toda ali lahko veste, kako paketno pretvoriti več datotek Excel v ločene datoteke CSV? V tem članku predstavljam kodo VBA za paketno pretvorbo vseh datotek Excel v mapi v datoteke CSV v Excelu.

Paketno pretvorite datoteke Excel iz mape v datoteke CSV z VBA

Pretvorite liste delovnega zvezka v ločene datoteke CSV s programom Kutools za Exceldobra ideja3


Paketno pretvorite datoteke Excel iz mape v datoteke CSV z VBA

V Excelu ni vgrajene funkcije, ki bi to nalogo lahko hitro rešila, razen VBA.

1. Omogočite Excel in pritisnite Alt + F11 tipke odprte Microsoft Visual Basic za aplikacije okno.

2. klik Vstavi > Moduli ustvariti nov modul.

3. Kopirajte spodnjo kodo in jih prilepite v novo okno modula.

VBA: paketno pretvorite datoteke Excel v 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. Pritisnite F5 v prvem pogovornem oknu izberite mapo z datotekami Excel, ki jih želite pretvoriti v datoteke CSV.
paket dokumentov v csv 1

5. klik OK, nato v drugem pojavnem pogovornem oknu izberite mapo, kamor želite namestiti datoteke CSV.
paket dokumentov v csv 2

6. klik OK, zdaj so datoteke Excel v mapi pretvorjene v datoteke CSV in shranjene v drugo mapo.
paket dokumentov v csv 3


Pretvorite liste delovnega zvezka v ločene datoteke CSV s programom Kutools za Excel

Kot že vemo, lahko celotno delovno knjigo v Excelu pretvorimo samo v eno datoteko CSV s funkcijo Shrani kot. Toda včasih želite en list pretvoriti v datoteko CSV, v tem primeru datoteko Razdeljeni delovni zvezek uporabnost Kutools za Excel vam lahko pomaga.

Kutools za Excel, z več kot 300 priročne funkcije, vam olajša delo. 

Po namestitvi Kutools za Excel, naredite spodaj:(Brezplačno prenesite Kutools za Excel zdaj!)

1. Omogočite delovni zvezek, v katerega želite pretvoriti njegove liste kot ločene datoteke CSV, kliknite Kutools Plus > Delovni zvezek > Razdeljeni delovni zvezek.
paket dokumentov v csv 4

2. V Ljubljani Razdeljeni delovni zvezek pogovorno okno, označite ime lista, ki ga želite razdeliti (vsi listi so privzeto označeni), označite Shrani kot tip, izberite CSV (Macintosh) (* .CSV) s spustnega seznama.
paket dokumentov v csv 5

3. klik Split izstopiti a Brskaj po mapi pogovorno okno, izberite ali ustvarite mapo, v katero želite namestiti datoteke CSV.
paket dokumentov v csv 6

4. klik OK, zdaj je delovni zvezek razdeljen kot ločeni datoteki CSV.
paket dokumentov v csv 7


Najboljša orodja za pisarniško produktivnost

Kutools za Excel rešuje večino vaših težav in poveča vašo produktivnost za 80%

  • Ponovna uporaba: Hitro vstavite zapletene formule, grafikoni in vse, kar ste že uporabljali; Šifriraj celice z geslom; Ustvari poštni seznam in pošiljanje e-pošte ...
  • Vrstica Super Formula (enostavno urejanje več vrstic besedila in formule); Bralna postavitev (enostavno branje in urejanje velikega števila celic); Prilepite v filtrirani obseg...
  • Združi celice / vrstice / stolpce brez izgube podatkov; Vsebina razdeljenih celic; Združi podvojene vrstice / stolpce... prepreči podvojene celice; Primerjaj obsege...
  • Izberite Duplicate ali Unique Vrstice; Izberite prazne vrstice (vse celice so prazne); Super Find in Fuzzy Find v mnogih delovnih zvezkih; Naključna izbira ...
  • Natančna kopija Več celic brez spreminjanja sklica formule; Samodejno ustvarjanje referenc na več listov; Vstavi oznake, Potrditvena polja in še več ...
  • Izvleček besedila, Dodaj besedilo, Odstrani po položaju, Odstrani presledek; Ustvari in natisni vmesne seštevke strani Pretvarjanje med vsebino celic in komentarji...
  • Super filter (shranite in uporabite sheme filtrov za druge liste); Napredno razvrščanje glede na mesec / teden / dan, pogostost in drugo; Poseben filter s krepko, ležeče ...
  • Združite delovne zvezke in delovne liste; Spoji tabele na podlagi ključnih stolpcev; Razdelite podatke na več listov; Paketna pretvorba xls, xlsx in PDF...
  • Več kot 300 zmogljivih funkcij. Podpira Office / Excel 2007-2019 in 365. Podpira vse jezike. Preprosta namestitev v vašem podjetju ali organizaciji. Vse funkcije 30-dnevnega brezplačnega preskusa. 60-dnevno jamstvo za vračilo denarja.
zavihek kte 201905

Kartica Office prinaša vmesnik z zavihki v Office in poenostavi vaše delo

  • Omogočite urejanje in branje z zavihki v Wordu, Excelu, PowerPointu, Publisher, Access, Visio in Project.
  • Odprite in ustvarite več dokumentov v novih zavihkih istega okna in ne v novih oknih.
  • Poveča vašo produktivnost za 50%in vsak dan zmanjša na stotine klikov miške za vas!
dno pisarniške mize
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    TBenson85 · 2 months ago
    Hello, 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.
      TBenson85 · 2 months ago
      So, 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.  
  • To post as a guest, your comment is unpublished.
    Jared · 3 months ago
    This is amazing. Thank-you!
  • To post as a guest, your comment is unpublished.
    wilfried · 4 months ago
    top thanks :) 
  • To post as a guest, your comment is unpublished.
    Sen · 1 years ago
    I 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.
    Justin · 1 years ago
    Thank 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.
    Sam · 2 years ago
    Another 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:

    Cells.Select
    Selection.NumberFormat = "@"
  • To post as a guest, your comment is unpublished.
    Sam · 2 years ago
    Works 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.
      Carol · 1 years ago
      Have you found a way around this issue?
      • To post as a guest, your comment is unpublished.
        Clara · 1 years ago
        Carol,

        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.
          Sonya · 1 years ago
          Thank you so much! This has saved me so much time!!
  • To post as a guest, your comment is unpublished.
    Aman · 2 years ago
    Ty it really works dear !!