Macro unisci file cvs e salta prima riga

Anonimo
2015-10-25T22:47:45+00:00

Buonasera a tutti,

grazie alla rete son riuscito a creare una macro che unisce tutti i files *csv presenti nella cartella dove risiede il file Excel a cui è assegnata la macro stessa.

Premesso che i files cvs hanno tutti la medesima riga di intestazione vorrei evitare che questa venga importata tante volte quanti sono i files da importare.

Come dovrei modificare il mio codice?

Grazie anticipatamente

Stefano

Questo il codice

Sub UnisciCSV()

  Dim FName As Variant, R As Long, AppPath As String

  R = 1

  AppPath = Application.ActiveWorkbook.Path & ""

  FName = Dir(Application.ActiveWorkbook.Path & "" & "*.csv")

  Do While FName <> ""

    ImportaCsvFile AppPath & FName, ActiveSheet.Cells(R, 1)

    R = ActiveSheet.UsedRange.Rows.Count + 1

    FName = Dir

  Loop

End Sub

Sub ImportaCsvFile(FileName As Variant, Position As Range)

  With ActiveSheet.QueryTables.Add(Connection:= _

      "TEXT;" & FileName _

      , Destination:=Position)

      .Name = Replace(FileName, ".csv", "")

      .FieldNames = True

      .RowNumbers = False

      .FillAdjacentFormulas = False

      .RefreshOnFileOpen = False

      .BackgroundQuery = True

      .RefreshStyle = xlInsertDeleteCells

      .SavePassword = False

      .SaveData = True

      .AdjustColumnWidth = True

      .TextFilePromptOnRefresh = False

      .TextFilePlatform = 850

      .TextFileStartRow = 1

      .TextFileParseType = xlDelimited

      .TextFileTextQualifier = xlTextQualifierDoubleQuote

      .TextFileConsecutiveDelimiter = False

      .TextFileTabDelimiter = True

      .TextFileSemicolonDelimiter = False

      .TextFileCommaDelimiter = False

      .TextFileSpaceDelimiter = False

      .TextFileOtherDelimiter = ";"

      .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _

        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _

        , 1, 1)

      .Refresh BackgroundQuery:=False

  End With

End Sub

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
2015-10-26T01:11:04+00:00

Ciao Stefano,

Prova a sostituire il codice della procedura UnisciCSV con la seguente versione:

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

Option Explicit

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

Public Sub UnisciCSV()

    Dim FName As Variant

    Dim R As Long, i As Long

    Dim AppPath As String

    R = 1

    AppPath = Application.ActiveWorkbook.Path & " \ "

    FName = Dir(AppPath & "*.csv")

    Do While FName <> ""

  i = i + 1

        ImportaCsvFile AppPath & FName, ActiveSheet.Cells(R, 1)

        If i > 1 Then

ActiveSheet.Rows(R).Delete

End If

        R = ActiveSheet.UsedRange.Rows.Count + 1

        FName = Dir

    Loop

End Sub

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

Le modifiche sono evidenziate in grassetto.

===

Regards,

Norman

La risposta è stata utile?

0 commenti Nessun commento

4 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2015-10-27T16:40:13+00:00

    Ciao Mauro,

    Fatto.

    Sempre diligente! 

    Grazie!

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2015-10-27T16:37:37+00:00

    Fatto.

    Ciao.

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2015-10-27T16:34:20+00:00

    Ciao Stefano,

    Mi fa piacere che hai risolto il problema e ti ringrazio per il cortese riscontro.

    Per chiudere questo thread, vorrei chiederti gentilmente di contrasegnare la mia risposta come Risposta preferita. In questo modo, tu aiuterai anche coloro che potessero cercare soluzioni ai problemi simili negli archivi della Community.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2015-10-27T16:30:47+00:00

    Ho apportato le modifiche suggerite ed il tutto va che una meraviglia.

    Grazie infinite

    Questo il codice corretto:

    Sub UnisciCSV()

      Dim FName As Variant, R As Long, i As Long, AppPath As String

      R = 1

      AppPath = Application.ActiveWorkbook.Path & ""

      FName = Dir(Application.ActiveWorkbook.Path & "" & "*.csv")

      Do While FName <> ""

        i = i + 1

        ImportaCsvFile AppPath & FName, ActiveSheet.Cells(R, 1)

        If i > 1 Then

                ActiveSheet.Rows(R).Delete

            End If

        R = ActiveSheet.UsedRange.Rows.Count + 1

        FName = Dir

      Loop

    End Sub

    Sub ImportaCsvFile(FileName As Variant, Position As Range)

      With ActiveSheet.QueryTables.Add(Connection:= _

          "TEXT;" & FileName _

          , Destination:=Position)

          .Name = Replace(FileName, ".csv", "")

          .FieldNames = True

          .RowNumbers = False

          .FillAdjacentFormulas = False

          .RefreshOnFileOpen = False

          .BackgroundQuery = True

          .RefreshStyle = xlInsertDeleteCells

          .SavePassword = False

          .SaveData = True

          .AdjustColumnWidth = True

          .TextFilePromptOnRefresh = False

          .TextFilePlatform = 850

          .TextFileStartRow = 1

          .TextFileParseType = xlDelimited

          .TextFileTextQualifier = xlTextQualifierDoubleQuote

          .TextFileConsecutiveDelimiter = False

          .TextFileTabDelimiter = True

          .TextFileSemicolonDelimiter = False

          .TextFileCommaDelimiter = False

          .TextFileSpaceDelimiter = False

          .TextFileOtherDelimiter = ";"

          .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _

            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _

            , 1, 1)

          .Refresh BackgroundQuery:=False

      End With

    End Su

    La risposta è stata utile?

    0 commenti Nessun commento