【问题标题】:vba importing txt file ignoring special linesvba导入txt文件忽略特殊行
【发布时间】:2014-11-10 20:00:08
【问题描述】:

我有一个 vba 代码可以将多个 txt 文件导入 excel。每个 txt 文件都包含几行以 # 开头的行。这几行我想跳过并在第一行开始导入,没有#

我用来导入文件的代码如下:

Sub Import_Text_Files()
  Dim sPath As String
  Dim oPath, oFile, oFSO As Object
  Dim r, iRow As Long
  Dim wbImportFile As Workbook
  Dim wsDestination As Worksheet

  sPath = "C:\txt-files\"

  Set wsDestination = ThisWorkbook.Sheets("Daten")

  i = 1

  Set oFSO = CreateObject("Scripting.FileSystemObject")
  Set oPath = oFSO.GetFolder(sPath)
  Application.ScreenUpdating = False

  For Each oFile In oPath.Files
    r = 4
    If LCase(Right(oFile.Name, 4)) = ".txt" Then
        Workbooks.OpenText fileName:=oFile.Path, Origin:=65001, StartRow:=1, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True

        Set wbImportFile = ActiveWorkbook
        For iRow = 1 To wbImportFile.Sheets(1).UsedRange.Rows.Count
            wbImportFile.Sheets(1).UsedRange.Rows(iRow).Copy wsDestination.Cells(r, i)
            r = r + 1
            End If
        Next iRow
        wbImportFile.Close False
        Set wbImportFile = Nothing
      End If
    i = i + 7

    Next oFile

  End Sub

我尝试使用INSTR,但没有成功。

谁能帮帮我?

【问题讨论】:

  • 为什么不全部导入excel,如图here,然后在excel中使用自动过滤器删除所有以“#”开头的行会比遍历文本文件中的每一行要快得多?跨度>
  • 查看我在上面评论中提到的链接中的Open the text file in memory部分
  • 谢谢,我试试这个!

标签: vba excel


【解决方案1】:

我找到了删除所有空单元格和以#开头的单元格的解决方案

Sub Read_Text_Files()

    Dim sPath As String
    Dim oPath, oFile, oFSO As Object
    Dim r, iRow As Long
    Dim wbImportFile As Workbook
    Dim wsDestination As Worksheet

    sPath = "C:\Daten\"

    Set wsDestination = ThisWorkbook.Sheets("Daten")

    i = 1
    j = 1

    Set oFSO = CreateObject("Scripting.FileSystemObject")

        Set oPath = oFSO.GetFolder(sPath)
        Application.ScreenUpdating = False

        For Each oFile In oPath.Files
        r = 4

            If LCase(Right(oFile.Name, 4)) = ".txt" Then

                Workbooks.OpenText fileName:=oFile.Path, Origin:=65001, StartRow:=1, DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, FieldInfo:=Array(1, 1), _
                TrailingMinusNumbers:=True

                Set wbImportFile = ActiveWorkbook
                For iRow = 1 To wbImportFile.Sheets(1).UsedRange.Rows.Count
                    wbImportFile.Sheets(1).UsedRange.Rows(iRow).Copy wsDestination.Cells(r, i)
                    r = r + 1
                Next iRow
                wbImportFile.Close False
                Set wbImportFile = Nothing
            End If
        i = i + 7
        j = j + 1

    Next oFile

    Dim rng As Range

    Set rng = ActiveCell

    For k = 1 To wsDestination.UsedRange.Columns.Count
        For l = 1 To 20
            wsDestination.Cells(4, k).Select

            If IsEmpty(wsDestination.Cells(4, k)) Then
                Cells(4, k).Delete xlShiftUp
            End If

            If InStr(wsDestination.Cells(4, k).Value, "#") = 0 Then
            Else
                Cells(4, k).Delete xlShiftUp
            End If
        Next l
    Next k

End Sub

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2023-02-06
    相关资源
    最近更新 更多