Import Multiple Text Files in the same excel Sheet

前端 未结 2 1603
清酒与你
清酒与你 2021-01-16 19:55

I\'m running a Macro on Excel to import multiple .txt files and with a filter set to the filename, so it acts like a wildcard. Every file has the same layout, it\'s Semicolo

相关标签:
2条回答
  • 2021-01-16 20:40

    I haven't tested but it seems like replacing :

    Sub Importar_PORT(iFullFilePath As String, iFileNameWithoutExtension)
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & iFullFilePath, _
            Destination:=Range("$A$1"))
    

    with :

    Sub Importar_PORT(iFullFilePath As String, iFileNameWithoutExtension)
    
        afterLast = Cells(Rows.Count, 1).End(xlUp).Row + 1
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & iFullFilePath, _
            Destination:=Range("$A$" & afterLast))
    

    would work fine.

    0 讨论(0)
  • 2021-01-16 20:59

    Adding a check if Range("A1") is empty so it starts at A1 if A1 is empty...

    Tested and working:

    Sub Importar_PORT(iFullFilePath As String, iFileNameWithoutExtension)
    
        Dim lngStartRow As Long
        With ActiveSheet
            If .Range("A1") = "" Then
                lngStartRow = 1
            Else
                lngStartRow = .Range("A" & .Rows.Count).End(xlUp).row + 1
            End If
        End With
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & iFullFilePath, _
            Destination:=Range("$A$" & lngStartRow))
    
    0 讨论(0)
提交回复
热议问题