简体   繁体   中英

How to loop through directory and export content of excel files, workbook by workbook, sheet by sheet (VBA Macros)?

I am trying to export some tables from excel workbooks to txt files. I have managed to do this for a workbook whilst I have it open. My code will loop through the sheets of a workbook I have open, and export the content to txt files (different file for each sheet). I need it to loop through a directory of .xlsx files and do the same without any manual labor. Can anyone help?

Below is my code:

Sub Exportation()

    Dim directory As String
    Dim WS_Count As Integer, myFile As String, x As Integer
    Dim rng As Range, cellValue As Variant, i As Integer, j As Integer

    directory = "C:\Users\Mike\Desktop\Testing\" ' The starting directory

    Dim fso, newFile, folder, files
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(directory)
    Set files = folder.files

    For Each file In files
        WS_Count = ActiveWorkbook.Worksheets.Count
        For x = 1 To WS_Count

            myFile = "C:\Users\mohamednuri.beitelma\Desktop\" & Sheets(x).Name & ".txt"

            'Set rng = Selection
            Set rng = Sheets(x).Range("A1").CurrentRegion
            Open myFile For Output As #1
            cnt = Sheets(x).Cells.SpecialCells(xlCellTypeLastCell).Row

            For i = 1 To rng.Rows.Count
                For j = 1 To rng.Columns.Count

                    cellValue = rng.Cells(i, j).Value

                    If j = rng.Columns.Count Then
                        Print #1, cellValue
                    Else
                        Print #1, cellValue & "|",
                    End If

                Next j
            Next i
        Print #1, cnt & " -- " & DateTime.Now
    Close #1
Next x

Next

End Sub

Try something like this (untested):

Sub Exportation()

Dim directory As String
Dim wb As Workbook
Dim ws as worksheet
Dim WS_Count As Integer, myFile As String, x As Integer
Dim rng As Range, cellValue As Variant, i As Integer, j As Integer

directory = "C:\Users\Mike\Desktop\Testing\" ' The starting directory

Dim fso, newFile, folder, files
Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder(directory)
Set files = folder.files

For Each file In files
    set wb = workbooks.open(filename:=file.path)
    WS_Count = wb.Worksheets.Count
    For x = 1 To WS_Count

        myFile = "C:\Users\mohamednuri.beitelma\Desktop\" & Sheets(x).Name & ".txt"

        'Set rng = Selection
        Set rng = wb.Sheets(x).Range("A1").CurrentRegion
        Open myFile For Output As #1
        cnt = wb.Sheets(x).Cells.SpecialCells(xlCellTypeLastCell).Row

        For i = 1 To rng.Rows.Count
            For j = 1 To rng.Columns.Count

                cellValue = rng.Cells(i, j).Value

                If j = rng.Columns.Count Then
                    Print #1, cellValue
                Else
                    Print #1, cellValue & "|",
                End If

            Next j
        Next i
        Print #1, cnt & " -- " & DateTime.Now
    Close #1
    Next x

Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM