Unire in excel più fogli di diverse cartelle in un foglio di una nuova cartella

Anonimo
2016-06-23T11:40:40+00:00

Ciao a tutti,

spero che qualche anima pia mi aiuti passo passo, spiego la situazione:

quotidiamente nella cartella C:\Users\Utente\Documents\ del mio Windows devo creare diverse cartelle di lavoro excel il cui nome è 1.csv, 2.csv, 3.csv ... 112.csv, tutte con la stessa intestazione di colonna e una serie di 300 righe diverse, ora vorrei unire tutte le righe delle 100 e rotte cartelle create in un solo foglio di una nuova cartella denominata ad esempio unione.xlsx, è possibile?????

Grazie...

Vincenzo

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2016-06-30T06:27:15+00:00

Ciao Vincenzo,

Ciao, ho provato a scaricare di nuovo i file e li ho estratti su una cartella sul desktop, la cosa bella è che ora mi sviluppa i 124 files ma poi riepilogo non contiene dati... vedi screenshot

Durante la notte un pensiero spiacevole mi è venuto in mente: non è possibile per caso che tu stessi provando di eseguire la seconda routine Diagnostica anzichè la procedura Tester? La procedura Diagnostica era intesa solo per confermare la presenza dei file CSV nella directory stipulata.

===

Regards,

Norman

![](http://fud.community.services.support.microsoft.com/Fud/FileDownloadHandler.ashx?fid=f7d30bf4-60c7-433a-aeb0-4f21e6dc9edc)

La risposta è stata utile?

0 commenti Nessun commento

43 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2016-06-27T12:27:45+00:00

    Ciao Vincenzo,

    il codice che hai creato funziona, è bellissimo, non avrei mai saputo crearlo! L'unico problema è che le 5 colonne ("Nome;"Indirizzo";"CAP";"Città";"Telefono") si sono accorpate in due, nella prima trovo nome e parte di indirizzo e nella seconda parte di indirizzo cap città e telefono, in più nella colonna C c'è l'indicazione del file originale.

    Non c'è un modo per lasciare le colonne nel modo originale, quindi

    A Nome

    B Indirizzo

    C CAP

    D Città

    E Telefono.

    Mi spiace ma non ho capito!

    Se i file csv abbiano cinque colonne, le stesse cinque colonne si dovrebbero trovare nel nuovo foglio di riepilogo. Ho aggiunto una colonna in più per anche indicare il nome del file originario ma posso facilmente modificare il codice per eliminarla se non fosse utile. Ad ogni modo, per risolvere il problema, ti chiederei gentilmente di caricare un esempio di un file csv e un esempio dei risultati  che ottieni e dei risultati voluti.  In ciascun caso, credo sia necessario solo includere le intestazioni.

    ===

    Regards,

    Norman

    ![](http://fud.community.services.support.microsoft.com/Fud/FileDownloadHandler.ashx?fid=42b51ef7-1617-4ce4-85ce-92d7a69bd2c8)

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-06-27T10:43:25+00:00

    Ciao Vincenzo,

    Prova qualcosa del genere:

    • Alt+F11 per aprire l'editor di VBA
    • Menù | Inserisci | Modulo (oppure Alt+IM) per inserire un nuovo modulo di codice
    • Nel nuovo modulo vuoto, incolla il seguente codice:

    '===========>>

    Option Explicit

    '----------->>

    Public Sub Tester()

        Dim FSO As Object

        Dim oFile As Object

        Dim oFiles As Object

        Dim oFolder As Object

        Dim srcWb As Workbook, destWB As Workbook

        Dim srcSH As Worksheet, destSH As Worksheet

        Dim srcRng As Range, destRng As Range

        Dim arrIn() As Variant, arrHeaders() As Variant

        Dim iCtr As Long, jCtr As Long

        Dim i As Long, j As Long

        Dim LRow As Long, LCol As Long

        Dim sName As String

        Const sPercorso As String = "**C:\Users\Utente\Documents**"

        Const sSummary As String = "Riepilogo"

        Const sNameType As String = "*.csv"

        Set destWB = ThisWorkbook

        With destWB

            On Error Resume Next

            With Application

                .ScreenUpdating = False

                .DisplayAlerts = False

                .Sheets(sSummary).Delete

                .DisplayAlerts = True

                Err.Clear

            End With

            On Error GoTo XIT

            Set destSH = destWB.Sheets.Add(after:=.Sheets(.Sheets.Count))

        End With

        destSH.Name = sSummary

        Set FSO = CreateObject("Scripting.FileSystemObject")

        Set oFolder = FSO.GetFolder(sPercorso)

        Set oFiles = oFolder.Files

        For Each oFile In oFiles

            With oFile

                If .Name Like sNameType Then

                    Set srcWb = Workbooks.Open(oFile)

                    Set srcSH = srcWb.Sheets(1)

                    With srcSH

                        LRow = LastRow(srcSH, .Columns("A:A"))

                        LCol = LastCol(srcSH)

                        Set srcRng = .Range("A2").Resize(LRow - 1, LCol)

                        jCtr = iCtr

                        iCtr = iCtr + LRow - 1

                        If Not IsArrayAllocated(arrHeaders) Then

                            arrHeaders = srcRng.Rows(0).Value

                            ReDim Preserve arrHeaders(1 To 1, 1 To LCol + 1)

                            arrHeaders(1, LCol + 1) = "File Originale"

                        End If

                    End With

                    ReDim Preserve arrIn(1 To LCol + 1, 1 To iCtr)

                    For i = 1 To LRow - 1

                        For j = 1 To LCol

                            arrIn(j, jCtr + i) = srcRng.Cells(i, j).Value

                        Next j

                        arrIn(j, jCtr + i) = oFile.Name

                    Next i

                    srcWb.Close savechanges:=False

                End If

            End With

        Next oFile

        With destSH

            Set destRng = destSH.Range("A2").Resize(iCtr, j)

            destRng.Value = Application.Transpose(arrIn)

            With destRng.Rows(0)

                .Value = arrHeaders

                .Font.Bold = True

            End With

            .UsedRange.EntireColumn.AutoFit

        End With

    XIT:

        Application.ScreenUpdating = True

    End Sub

    '--------->>

    Public Function LastRow(SH As Worksheet, _

                            Optional Rng As Range, _

                            Optional minRow As Long = 1)

        If Rng Is Nothing Then

            Set Rng = SH.Cells

        End If

        On Error Resume Next

        LastRow = Rng.Find(What:="*", _

                           after:=Rng.Cells(1), _

                           Lookat:=xlPart, _

                           LookIn:=xlFormulas, _

                           SearchOrder:=xlByRows, _

                           SearchDirection:=xlPrevious, _

                           MatchCase:=False).Row

        On Error GoTo 0

        If LastRow < minRow Then

            LastRow = minRow

        End If

    End Function

    '--------->>

    Public Function LastCol(SH As Worksheet, _

                            Optional Rng As Range)

        If Rng Is Nothing Then

            Set Rng = SH.Cells

        End If

        On Error Resume Next

        LastCol = Rng.Find(What:="*", _

                           after:=Rng.Cells(1), _

                           Lookat:=xlPart, _

                           LookIn:=xlFormulas, _

                           SearchOrder:=xlByColumns, _

                           SearchDirection:=xlPrevious, _

                           MatchCase:=False).Column

        On Error GoTo 0

    End Function

    '--------->>

    Public Function IsArrayAllocated(Arr As Variant) As Boolean

        On Error Resume Next

        IsArrayAllocated = IsArray(Arr) And _

                           Not IsError(LBound(Arr, 1)) And _

                           LBound(Arr, 1) <= UBound(Arr, 1)

    End Function

    '<<========= 

    • Alt+Q per chiudere l'editor di VBA e tornare a Excel
    • Salva il file con l’estensione xlsm
    • Alt+F8 per aprire  la finestra di gestione delle macro
    • Seleziona Tester | Esegui

    ===

    Regards,

    Norman

    ![](http://fud.community.services.support.microsoft.com/Fud/FileDownloadHandler.ashx?fid=95bb98b5-2ef8-4eed-a18d-7a40ff4626c3)

    Ciao,

    il codice che hai creato funziona, è bellissimo, non avrei mai saputo crearlo! L'unico problema è che le 5 colonne ("Nome;"Indirizzo";"CAP";"Città";"Telefono") si sono accorpate in due, nella prima trovo nome e parte di indirizzo e nella seconda parte di indirizzo cap città e telefono, in più nella colonna C c'è l'indicazione del file originale.

    Non c'è un modo per lasciare le colonne nel modo originale, quindi

    A Nome

    B Indirizzo

    C CAP

    D Città

    E Telefono.

    Grazie mille e resto in attesa.

    Ciao

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2016-06-23T14:23:05+00:00

    Ciao Vincenzo,

    Prova qualcosa del genere:

    • Alt+F11 per aprire l'editor di VBA
    • Menù | Inserisci | Modulo (oppure **Alt+IM)**per inserire un nuovo modulo di codice
    • Nel nuovo modulo vuoto, incolla il seguente codice:

    '===========>>

    Option Explicit

    '----------->>

    Public Sub Tester()

        Dim FSO As Object

        Dim oFile As Object

        Dim oFiles As Object

        Dim oFolder As Object

        Dim srcWb As Workbook, destWB As Workbook

        Dim srcSH As Worksheet, destSH As Worksheet

        Dim srcRng As Range, destRng As Range

        Dim arrIn() As Variant, arrHeaders() As Variant

        Dim iCtr As Long, jCtr As Long

        Dim i As Long, j As Long

        Dim LRow As Long, LCol As Long

        Dim sName As String

        Const sPercorso As String = "**C:\Users\Utente\Documents**"

        Const sSummary As String = "Riepilogo"

        Const sNameType As String = "*.csv"

        Set destWB = ThisWorkbook

        With destWB

            On Error Resume Next

            With Application

                .ScreenUpdating = False

                .DisplayAlerts = False

                .Sheets(sSummary).Delete

                .DisplayAlerts = True

                Err.Clear

            End With

            On Error GoTo XIT

            Set destSH = destWB.Sheets.Add(after:=.Sheets(.Sheets.Count))

        End With

        destSH.Name = sSummary

        Set FSO = CreateObject("Scripting.FileSystemObject")

        Set oFolder = FSO.GetFolder(sPercorso)

        Set oFiles = oFolder.Files

        For Each oFile In oFiles

            With oFile

                If .Name Like sNameType Then

                    Set srcWb = Workbooks.Open(oFile)

                    Set srcSH = srcWb.Sheets(1)

                    With srcSH

                        LRow = LastRow(srcSH, .Columns("A:A"))

                        LCol = LastCol(srcSH)

                        Set srcRng = .Range("A2").Resize(LRow - 1, LCol)

                        jCtr = iCtr

                        iCtr = iCtr + LRow - 1

                        If Not IsArrayAllocated(arrHeaders) Then

                            arrHeaders = srcRng.Rows(0).Value

                            ReDim Preserve arrHeaders(1 To 1, 1 To LCol + 1)

                            arrHeaders(1, LCol + 1) = "File Originale"

                        End If

                    End With

                    ReDim Preserve arrIn(1 To LCol + 1, 1 To iCtr)

                    For i = 1 To LRow - 1

                        For j = 1 To LCol

                            arrIn(j, jCtr + i) = srcRng.Cells(i, j).Value

                        Next j

                        arrIn(j, jCtr + i) = oFile.Name

                    Next i

                    srcWb.Close savechanges:=False

                End If

            End With

        Next oFile

        With destSH

            Set destRng = destSH.Range("A2").Resize(iCtr, j)

            destRng.Value = Application.Transpose(arrIn)

            With destRng.Rows(0)

                .Value = arrHeaders

                .Font.Bold = True

            End With

            .UsedRange.EntireColumn.AutoFit

        End With

    XIT:

        Application.ScreenUpdating = True

    End Sub

    '--------->>

    Public Function LastRow(SH As Worksheet, _

                            Optional Rng As Range, _

                            Optional minRow As Long = 1)

        If Rng Is Nothing Then

            Set Rng = SH.Cells

        End If

        On Error Resume Next

        LastRow = Rng.Find(What:="*", _

                           after:=Rng.Cells(1), _

                           Lookat:=xlPart, _

                           LookIn:=xlFormulas, _

                           SearchOrder:=xlByRows, _

                           SearchDirection:=xlPrevious, _

                           MatchCase:=False).Row

        On Error GoTo 0

        If LastRow < minRow Then

            LastRow = minRow

        End If

    End Function

    '--------->>

    Public Function LastCol(SH As Worksheet, _

                            Optional Rng As Range)

        If Rng Is Nothing Then

            Set Rng = SH.Cells

        End If

        On Error Resume Next

        LastCol = Rng.Find(What:="*", _

                           after:=Rng.Cells(1), _

                           Lookat:=xlPart, _

                           LookIn:=xlFormulas, _

                           SearchOrder:=xlByColumns, _

                           SearchDirection:=xlPrevious, _

                           MatchCase:=False).Column

        On Error GoTo 0

    End Function

    '--------->>

    Public Function IsArrayAllocated(Arr As Variant) As Boolean

        On Error Resume Next

        IsArrayAllocated = IsArray(Arr) And _

                           Not IsError(LBound(Arr, 1)) And _

                           LBound(Arr, 1) <= UBound(Arr, 1)

    End Function

    '<<========= 

    • Alt+Q per chiudere l'editor di VBA e tornare a Excel
    • Salva il file con l’estensione xlsm
    • Alt+F8 per aprire  la finestra di gestione delle macro
    • Seleziona Tester | Esegui

    ===

    Regards,

    Norman

    ![](http://fud.community.services.support.microsoft.com/Fud/FileDownloadHandler.ashx?fid=95bb98b5-2ef8-4eed-a18d-7a40ff4626c3)

    La risposta è stata utile?

    0 commenti Nessun commento