【问题标题】:Import Multiple Text Files in the same excel Sheet在同一个excel工作表中导入多个文本文件
【发布时间】:2023-03-21 04:01:01
【问题描述】:

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

宏工作正常,除了它“并排”或“水平”导入文件。不是导入“下”的下一个文件(例如,第一个文件上升到第 10 行,然后下一个文件从第 11 行开始导入),而是在下一个列中开始导入(第一个文件上升到列“K”,下一个开始在列 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

【问题讨论】:

    标签: vba excel import


    【解决方案1】:

    添加检查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))
    

    【讨论】:

    • 完美!像魅力一样工作!再次非常感谢你,伙计!也谢谢你,@BernardSaucier!
    【解决方案2】:

    我还没有测试,但它似乎替换

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

    正常工作。

    【讨论】:

    • 它确实有效,但不是很好,它确实在下一个文件下导入了下一个文件,但它删除的行数超过了应有的行数。如果你看,我有一个“Do While”正在运行,所以它只会保留一个标题,所以它检查第一个列,不包括第一个单元格,如果它不是数字,它的标题,所以它被删除。使用您的代码删除大量行..任何线索?
    • 可能是因为 Sub Importar_PORT 是从模块运行的,并且您应该引用工作表:afterLast = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 1。否则我不知道为什么...
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    相关资源
    最近更新 更多