在同一个Excel工作表中导入多个文本文件

时间:2014-02-06 16:23:47

标签: excel vba excel-vba import

我正在Excel上运行宏来导入多个.txt文件并使用过滤器设置为文件名,因此它就像一个通配符。每个文件都有相同的布局,它是分号分隔的,有一个标题和11个列。

除了导入“并排”或“水平”文件外,宏工作正常。而不是导入下一个文件“下”(比如,第一个文件上升到第10行,然后下一个文件开始在第11行导入),它开始在下一个colunm中导入(第一个上升到colunm“K”,下一个开始导入colunm L)。

我该如何解决?下面是代码:

Sub Abrir_PORT()

    Dim Caminho As String
    Caminho = Sheets("DADOS").Cells(5, 5).Value
    Sheets("PORT").Select

    Dim FS
    Set FS = CreateObject("Scripting.FileSystemObject")

    Dim Filter As String: Filter = "ATENTO_TLMKT_REC*.txt"
    Dim dirTmp As String

    If FS.FolderExists(Caminho) Then
        dirTmp = Dir(Caminho & "\" & Filter)
        Do While Len(dirTmp) > 0
            Call Importar_PORT(Caminho & "\" & dirTmp, _
                            Left(dirTmp, InStrRev(dirTmp, ".") - 1))
            dirTmp = Dir
        Loop
    End If

End Sub

Sub Importar_PORT(iFullFilePath As String, iFileNameWithoutExtension)

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & iFullFilePath, _
        Destination:=Range("$A$1"))
        .Name = iFileNameWithoutExtension
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    iRow = 2

    Do While Sheets("PORT").Cells(iRow, 1) <> ""

                If Cells(iRow, 2) = IsNumber Then

                Else

                Rows(iRow).Select
                Selection.EntireRow.Delete

                iRow = iRow - 1
                contagem = contagem + 1

                End If

 iRow = iRow + 1

 Loop

    End With

End Sub

2 个答案:

答案 0 :(得分:0)

我没有测试,但似乎替换

Sub Importar_PORT(iFullFilePath As String, iFileNameWithoutExtension)

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

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

正常工作。

答案 1 :(得分:0)

如果Range("A1")为空,则添加一项检查,如果A1为空则从A1开始...

经过测试和工作:

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