2014-02-06 78 views
0

我正在Excel上運行一個宏來導入多個.txt文件,並將過濾器設置爲文件名,因此它的行爲就像通配符。每個文件都具有相同的佈局,分號由分號組成,有一個標題和11個柱。在同一個Excel中導入多個文本文件

宏工作正常,除了導入文件「並排」或「水平」。而不是導入下面的文件「下」(例如,第一個文件上行到第10行,然後下一個文件開始在第11行導入),它開始導入下一個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 

回答

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

完美!打扮得像一個魅力!非常感謝你,男人!還有,謝謝你,@BernardSaucier! –

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

做工精細。

+0

它沒有工作,但不porpely,它導入下一個文件下一個文件,但刪除更多的行比應該。如果你看,我有一個「Do While」運行,所以它只保留一個頭,所以它檢查第一個柱,不包括第一個單元,如果它不是數字,它的頭,它的刪除。用你的代碼刪除噸的行..任何線索? –

+0

也許是因爲'Sub Importar_PORT'是從一個模塊運行的,並且你應該把一個對工作表的引用:'afterLast = ActiveSheet.Cells(ActiveSheet.Rows.Count,1).End(xlUp).Row + 1'。否則我不知道爲什麼...... –

相關問題