Loncat ke daftar isi utama

Bagaimana cara cepat mengimpor banyak file csv / teks / xml di Excel?

Di Excel, Anda mungkin telah terikat untuk menyimpan buku kerja sebagai file csv, file teks atau file xml, tetapi pernahkah Anda mencoba mengimpor beberapa file csv / teks / xml dari folder ke dalam buku kerja atau lembar kerja? Pada artikel ini, saya memperkenalkan beberapa metode untuk mengimpor secara batch dengan cepat.

Impor beberapa file teks dari folder ke setiap lembar kerja buku kerja dengan VBA

Impor beberapa file csv dari folder ke dalam satu lembar dengan VBA

Impor beberapa file xml dari folder ke dalam satu lembar dengan VBA

Impor atau gabungkan beberapa file xml / csv ke dalam lembaran atau buku kerja dengan Kutools for Excel ide bagus3

Ekspor setiap lembar sebagai csv / teks / pdf ke folder dengan Kutools for Excelide bagus3


Untuk mengimpor file teks dari folder ke buku kerja, Anda dapat menggunakan VBA di bawah ini untuk menanganinya dengan cepat.

1. Aktifkan buku kerja kosong, dan tekan Alt + F11 kunci untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. klik Menyisipkan > Modul, dan tempelkan VBA ke file Modul jendela.

VBA: Impor semua file teks dari folder ke dalam buku kerja

Sub LoadPipeDelimitedFiles()
'UpdatebyKutoolsforExcel20151214
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\*.txt")
    Do While xFile <> ""
        xCount = xCount + 1
        Sheets(xCount).Select
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
          & xStrPath & "\" & xFile, Destination:=Range("A1"))
            .Name = "a" & xCount
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
            xFile = Dir
        End With
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files txt", , "Kutools for Excel"
End Sub

3. tekan F5 kunci atau Run untuk menjalankan VBA, dan pilih folder tempat Anda ingin mengimpor file teks dari dalam dialog yang muncul. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 1

4. Dan klik OK, dan setiap file teks di folder yang dipilih telah diimpor ke dalam satu lembar kerja dari buku kerja yang aktif. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 2doc mengimpor beberapa teks csv xml 3

Gabungkan beberapa lembar / Buku Kerja dengan mudah menjadi satu lembar atau Buku Kerja

Untuk menggabungkan beberapa lembar atau buku kerja ke dalam satu lembar atau buku kerja mungkin membosankan di Excel, tapi dengan Menggabungkan Fungsi di Kutools for Excel, Anda bisa menggabungkan lusinan lembar / buku kerja menjadi satu lembar atau buku kerja, juga, Anda bisa menggabungkan lembaran menjadi satu dengan beberapa klik saja.  Klik untuk uji coba gratis 30 hari berfitur lengkap!
menggabungkan lembaran
 
Kutools for Excel: dengan lebih dari 300 add-in Excel yang praktis, gratis untuk dicoba tanpa batasan dalam 30 hari.

Untuk mengimpor semua file csv dari folder menjadi satu lembar, Anda dapat menggunakan kode VBA di bawah ini.

1. Aktifkan lembar kerja kosong, dan tekan Alt + F11 kunci untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. klik Menyisipkan > Modul, dan tempelkan di bawah VBA ke file Modul jendela.

VBA: Impor file csv dari folder ke dalam satu lembar kerja

Sub ImportCSVsWithReference()
'UpdatebyKutoolsforExcel20151214
    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then xSht.UsedRange.Clear
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.csv")
    Do While xFile <> ""
        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
        Columns(1).Insert xlShiftToRight
        Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
        ActiveSheet.UsedRange.Copy xSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
        xWb.Close False
        xFile = Dir
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files csv", , "Kutools for Excel"
End Sub

3. tekan F5 kunci atau klik Run untuk menjalankan VBA, dan sebuah dialog muncul untuk memilih folder tempat Anda ingin mengimpor semua file csv. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 4

4. klik OK, dan dialog muncul untuk mengingatkan Anda jika menghapus isi lembar kerja aktif sebelum mengimpor, di sini saya klik Yes. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 5

Setelah mengklik Yes, semua file csv di folder yang dipilih diimpor ke sheet saat ini, dan data tempat dari Kolom A ke kanan. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 6doc mengimpor beberapa teks csv xml 7

olymp trade indonesiaTip: Jika Anda ingin menempatkan file csv secara horizontal di lembar kerja, Anda dapat menggunakan VBA di bawah ini.

Sub ImportCSVsWithReferenceI()
'UpdatebyKutoolsforExcel20151214
    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then
        xSht.UsedRange.Clear
        xCount = 1
    Else
        xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    End If
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.csv")
    Do While xFile <> ""
        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
        Rows(1).Insert xlShiftDown
        Range("A1") = ActiveSheet.Name
        ActiveSheet.UsedRange.Copy xSht.Cells(1, xCount)
        xWb.Close False
        xFile = Dir
        xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files csv", , "Kutools for Excel"
End Sub 

doc mengimpor beberapa teks csv xml 8


Jika Anda ingin mengimpor semua file XML dari folder ke dalam satu lembar, Anda dapat menggunakan kode VBA di bawah ini.

1. Pilih lembar kosong yang ingin Anda tempatkan data yang diimpor, dan tekan Alt + F11 kunci untuk mengaktifkan Microsoft Visual Basic untuk Aplikasi jendela.

2. klik Menyisipkan > Modul, tempel kode VBA ke file Modul jendela.

VBA: Impor file XML dari folder ke dalam lembar kerja.

Sub From_XML_To_XL()
'UpdatebyKutoolsforExcel20151214
    Dim xWb As Workbook
    Dim xSWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    Set xSWb = ThisWorkbook
    xCount = 1
    xFile = Dir(xStrPath & "\*.xml")
    Do While xFile <> ""
        Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
        xWb.Sheets(1).UsedRange.Copy xSWb.Sheets(1).Cells(xCount, 1)
        xWb.Close False
        xCount = xSWb.Sheets(1).UsedRange.Rows.Count + 2
        xFile = Dir()
    Loop
    Application.ScreenUpdating = True
    xSWb.Save
    Exit Sub
ErrHandler:
    MsgBox "no files xml", , "Kutools for Excel"
End Sub

3. klik Run tombol atau F5 kunci untuk menjalankan VBA, dan pilih folder di dialog popping, lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 9

4. klik OK, dan semua file XML di folder yang dipilih diimpor ke lembar aktif.


Jika Anda tidak terbiasa dengan VBA, jangan khawatir, di sini saya perkenalkan alat praktis - Kutools untuk Excel untukmu. Dengan kekuatannya Menggabungkan utilitas, Anda dapat dengan cepat menggabungkan beberapa file xml atau file csv ke dalam satu buku kerja atau satu lembar Excel.

Kutools untuk Excel, dengan lebih dari 300 fungsi praktis, membuat pekerjaan Anda lebih mudah. 

Setelah menginstal Kutools for Excel, lakukan seperti di bawah ini:(Unduh Gratis Kutools untuk Excel Sekarang!)

1. Excel aktif, dan klik Kutools Ditambah > Menggabungkan. Lihat tangkapan layar :
doc menggabungkan 1

2. Dan di langkah 1 dari Gabungkan dialog, pilih satu opsi pemisahan yang Anda butuhkan. Lihat tangkapan layar:
doc menggabungkan 2

3. klik Selanjutnya untuk pergi ke langkah 2 dari Gabungkanklik Add untuk menambahkan file dari berbagai folder atau file dari satu folder ke Buku catatan daftar, dan juga Anda dapat menentukan lembar yang ingin Anda gabungkan Lembar Kerja daftar bagian kanan. Lihat tangkapan layar:
doc kutools menggabungkan lembar 3

4. klik Selanjutnya ke satu langkah terakhir dari Menggabungkan, dan Anda dapat menentukan opsi penggabungan.
doc kutools menggabungkan lembar 4

5. klik Finish, sebuah dialog muncul untuk mengingatkan Anda memilih lokasi untuk menyimpan hasil gabungan yang baru. Lihat tangkapan layar:
doc menggabungkan 5

6. klik Save. Semua lembar tambahan telah digabungkan menjadi satu lembar baru.
doc menggabungkan 6

olymp trade indonesiaTip: Dengan Menggabungkan, Anda juga dapat menggabungkan beberapa file File CSV membentuk beberapa folder atau satu folder menjadi satu lembar atau workbook.


Jika Anda ingin mengekspor setiap lembar sebagai file csv / teks / pdf ke folder, Kutools untuk Excel'S Pisahkan Buku Kerja utilitas dapat membantu Anda.

Setelah pemasangan gratis Kutools for Excel, lakukan seperti di bawah ini:

1. Aktifkan buku kerja yang ingin Anda ekspor lembar kerjanya, dan klik Kutools Ditambah > Buku catatan > Pisahkan Buku Kerja. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 10

2. Dalam Pisahkan Buku Kerja dialog, Anda dapat memeriksa nama lembar yang perlu Anda ekspor, secara default semua lembar dicentang, dan periksa Tentukan format penyimpanan dan pilih format file yang ingin Anda simpan seperti dari daftar drop-down di bawah ini. Lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 11

3. klik Split dan pilih folder yang akan menyimpan file yang dipisahkan di Jelajahi Folder dialog, lihat tangkapan layar:

doc mengimpor beberapa teks csv xml 12

4. klik OK, sekarang semua lembar yang dicentang diekspor sebagai format file baru di folder yang dipilih.


Artikel Relatif:

Alat Produktivitas Kantor Terbaik

🤖 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
Comments (36)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bagaimana caranya menghilangkan header dari tiap-tiap file csv yang terbuka dalam worksheetnya
terima kasih
This comment was minimized by the moderator on the site
Hi there,this is a great tool, but I want to import the various XMl-Files into separate TAB-sheets. Is this possible as the XML's have different header ?
This comment was minimized by the moderator on the site
HelloThe instructions for importing multiple xmls into one tab of an excel document works but was wondering how to get it to line up the columns. My xmls don't all have the same tags. They are set up such that if the xml had no data for some headers(tags) then the header is missing from that xml. Is there a way to get the xmls to import so the same headers from each xml and associated data fall into the same column of excel?
This comment was minimized by the moderator on the site
Hi Experts

I am using the above code for importing multiple xml files into 1 worksheet using VBA however issue i am facing is when rows count reaches 650000 in a worksheet then this code doesn't process rest of the xml files in the folder. It gives an error "no files.xml". Require your kind support
This comment was minimized by the moderator on the site
Hi Team

I am using the code for importing Multiple XML files into single sheet with VBA however issue i am facing is when rows count reaches approximately 650000, then it doesn't processes rest of the xml files in the folder and gives an error that no xml files. Need your support to increase this count.
This comment was minimized by the moderator on the site
Hi, is there any way to import multiple csv files with semicolon as separator? Thank you!
PS Nice article!
This comment was minimized by the moderator on the site
Hello - I've used your VBA codes to extract data from multiple CSV files to excel file (the code on this page) and convert csv files to excel files ( this one: https://www.extendoffice.com/documents/excel/4615-excel-batch-convert-csv-to-xls-xlsx.html), with great results. They helped me save a lot of time.

However, I notice a common problem with both of these types of codes. To clarify, my system is set up to use the European standards for dates, while some of the CSV files I received for my work contain dates in US standards. The first problem is, when I extract or convert data from a CSV file that contains dates in US format, all of those dates are reversed (matching the EU standards used by my system). This is great but it also caused me troubles since I didn't know the codes would reverse the dates for me, so I went on ahead and did the same thing again. The second problem is, for the CSV files that contain dates already in the same format as the one used by my system (EU standards), only the ambiguous dates are reversed (i.e 04/05/2019 - 05/04/2019), while the ones that are too obvious, remain unchanged (i.e 30/04/2019).

What I would like the codes to do, is the exact same thing as they are shown here, only that they should copy and paste the data (especially dates) in the exact formats used in the original files. This would help prevent any possible confusions and mistakes. I would like to learn VBA so I can one day write my own codes, but for now, I'm not even able to modify parts of the existing codes to suit my needs. So if you can help, please tell me where I should put the modified codes (that you come up with) to the existing codes. I appreciate all feedback & support I can get. Thank you all!
This comment was minimized by the moderator on the site
Hi Marshall, in the Workbooks.Open method, add in the option Local:=True.

i.e.
Set xWb = Workbooks.Open(xStrPath & "\" & xFile, Local:=True)
This comment was minimized by the moderator on the site
Hi Robert,
It's me again. It took me a while to actually have the time to figure out which part of the code the "Local:True" part should be added to. The result turned out great as the dates are no longer reversed. Thank you!
For anyone having the same problem, just change this line:
Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)

To this:
Set xWb = Workbooks.Open(xStrPath & "\" & xFile, Local:=True)
This comment was minimized by the moderator on the site
Thank you very much Robert. Sorry I couldn't reply to you any earlier. I didn't get any notification until now. I will try this out and come back to you later to let you know if this works.
This comment was minimized by the moderator on the site
Hi - I'm using the import all csv files into one file listed above "Import Multiple Csv Files From A Folder Into A Single Sheet With VBA"- i'd like to define the folder it collects the data from without having to manually choose it. Can this be done? thanks - SW.
This comment was minimized by the moderator on the site
Hi, Scott W, I found a VBA code may can help you.
Option Explicit

Sub ImportCSVsWithReference()
'Author: Jerry Beaucaire
'Date: 10/16/2010
'Summary: Import all CSV files from a folder into a single sheet
' adding a field in column A listing the CSV filenames

Dim wbCSV As Workbook
Dim wsMstr As Worksheet: Set wsMstr = ThisWorkbook.Sheets("Sheet1")
Dim fPath As String: fPath = " C:\Users\DT168\Desktop\New folder\" 'path to CSV files, include the final \
Dim fCSV As String

If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Clear?") _
= vbYes Then wsMstr.UsedRange.Clear

Application.ScreenUpdating = False 'speed up macro

fCSV = Dir(fPath & "*.csv") 'start the CSV file listing

Do While Len(fCSV) > 0
'open a CSV file
Set wbCSV = Workbooks.Open(fPath & fCSV)
'insert col A and add CSV name
Columns(1).Insert xlShiftToRight
Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
'copy date into master sheet and close source file
ActiveSheet.UsedRange.Copy wsMstr.Range("A" & Rows.Count).End(xlUp).Offset(1)
wbCSV.Close False
'ready next CSV
fCSV = Dir
Loop

Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
How to eliminate duplicate header and CSV file name column. Please do help....I have gone through several articles, but unfortunately all give same result.
This comment was minimized by the moderator on the site
Thank you. This site has been a big help. I have one issue I cannot figure out. I am trying to import multiple csv files into an excel separate sheets in excel and have each sheet renamed after the file name of the csv file. I know this was covered below for a txt file but I am working with csv files. Thanks in advance.
This comment was minimized by the moderator on the site
Hi! I used the code to merge multiple XML files into one, but unfortunately the columns got messed up. The 5 files being merged all had the same format. Is there anyway to fix this? I also was wondering if there was a way to get rid of the headers that are duplicated when the files are merged. Thank you!
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