Import Multiple Text Files in the same excel Sheet

旧时模样 提交于 2019-12-01 14:41:16

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))

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!