【问题标题】:Appending data from .txt to an excel sheet将 .txt 中的数据附加到 Excel 工作表
【发布时间】:2020-02-10 16:48:36
【问题描述】:

我有两个 .txt 文件。它们具有完全相同的格式和列。

第一个 .txt 文件如下所示:

这是我用来将数据从第一个 .txt 文件导出到 Excel 工作表的 VBA 代码(由 @FaneDuru 共享)

Sub CopyLessColumns()
 Dim strSpec As String, i As Long, colToRet As Long
 Dim arrSp As Variant, arrRez() As String, arrInt As Variant, j As Long
 Dim fso As Object, txtStr As Object, strText As String

  Set fso = CreateObject("Scripting.FileSystemObject")
  strSpec = "C:\Users\xxxxxxxxx\Desktop\Input.txt"
  If Dir(strSpec) <> "" Then                   
    Set txtStr = fso.OpenTextFile(strSpec)
        strText = txtStr.ReadAll
    txtStr.Close
 End If

  arrSp = Split(strText, vbCrLf)
  colToRet = 5                             'Number of columns you need
  ReDim arrRez(UBound(arrSp), colToRet - 1)
  For i = 0 To UBound(arrSp)
    arrInt = Split(arrSp(i), vbTab)
    If UBound(arrInt) > colToRet - 1 Then
        For j = 0 To colToRet - 1
            arrRez(i, j) = arrInt(j)
        Next j
    End If
  Next i
  ActiveSheet.Range(Cells(1, 1), Cells(UBound(arrRez, 1) + 1, UBound(arrRez, 2) + 1)).Value = arrRez
End Sub

运行上面的代码后,我的 excel 表格将如下所示:

但我不确定如何将第二个 .txt 文件中的数据附加到我现有的电子表格中。

下面是我的第二个 .txt 文件。相同的格式,相同的列号,只是不同的数据。

我想将第二个 .txt 文件中的数据附加到我的电子表格中,使其看起来像这样

如您所见,当我导入第二个.txt文件时,我想跳过标题行,直接导入第一行下面的数据。

另外,第一个 .txt 文件的行号可以随时更改,所以我不能只使用完全相同的代码并简单地更改最后一行

ActiveSheet.Range(Cells(2, 1), Cells(UBound(arrRez, 1) + 1, UBound(arrRez, 2) + 1)).Value = arrRez

ActiveSheet.Range(Cells(4, 1), Cells(UBound(arrRez, 1) + 1, UBound(arrRez, 2) + 1)).Value = arrRez

我也尝试使用

找到最后一行
lRow = Cells(Rows.Count, 1).End(xlUp).Row

然后,将最后一行改为

ActiveSheet.Range(Cells(lRow, 1), Cells(UBound(arrRez, 1) + 1, UBound(arrRez, 2) + 1)).Value = arrRez

但这也没有用。它只会用第二个 .txt 文件中的标题行覆盖电子表格中现有数据的最后一行

我尝试在网上查找,但没有找到与我在这里尝试做的类似的事情,所以,任何 cmets 将不胜感激!

【问题讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @EmmaG:请使用我最后的代码(答案)。我有一些时间,我编写了一个代码,可以独立于要处理的文本文件编号来完成您需要的事情。不需要两个单独的代码...
  • @FaneDuru 感谢您提供新代码。这些 cmets 真的在帮助我理解你在做什么。但是,只是为了澄清......我使用两个单独的代码/子的原因是因为我有两个 .txt 文件。他们的路径不同。我看到你的新代码只要我改变路径就可以自动附加第二个txt文件中的数据,所以谢谢你,这绝对有帮助
  • @EmmaG:我注释了所有代码以帮助您理解它。我会给你一个挑战:让Sub 接收一个字符串参数(strSpec),该参数从另一个子传递,你将在其中调用它两次,但使用不同的路径......并注意评论/删除行strSpec = "C:\Teste VBA Excel\TextFileTabDel.txt"...
  • @FaneDuru 是的,虽然略有不同,但这实际上与我现在正在研究的事情几乎相同。对于我的下一步,我试图将导入/附加代码放入循环函数中。提示用户选择文件,然后继续导入/追加,直到用户说停止。我会尝试:)

标签: excel vba append


【解决方案1】:

请使用此代码版本!对于要加载的许多文本文件,它都是相同的。它将只加载表头(当工作表为空时),然后只加载数据,没有表头:

Private Sub CopyLessColumns() 'it copies less columns than the txt file has
 Dim strSpec As String, i As Long, colToRet As Long, lastR As Long
 Dim arrSp As Variant, arrRez() As String, arrInt As Variant, j As Long, k As Long
 Dim fso As Object, txtStr As Object, strText As String 'no need of any reference

  Set fso = CreateObject("Scripting.FileSystemObject")
  strSpec = "C:\Teste VBA Excel\TextFileTabDel.txt"
  If Dir(strSpec) <> "" Then 'check if file exists
    Set txtStr = fso.OpenTextFile(strSpec)
        strText = txtStr.ReadAll
    txtStr.Close
  End If
  arrSp = Split(strText, vbCrLf)

    colToRet = 5 'Number of columns to be returned
    lastR = ActiveSheet.Range("A" & Rows.count).End(xlUp).Row 'last row in A:A
    'arrRez is dimensioned from 0 to UBound(arrSp) only for lastR = 1
    ReDim arrRez(IIf(lastR = 1, 0, 1) To UBound(arrSp), colToRet - 1)
    For i = IIf(lastR = 1, 0, 1) To UBound(arrSp) 'Only in case of larR = 1, the
                                                  'head of the table is load in arr
      arrInt = Split(arrSp(i), vbTab)  'each strText line is split in an array
      If UBound(arrInt) > colToRet - 1 Then
          For j = 0 To colToRet - 1
              arrRez(i, j) = arrInt(j) 'each array element is loaded in the arrRez
          Next j
      End If
    Next i
    'The array is dropped in the dedicated range (calculated using Resize):
    ActiveSheet.Range("A" & IIf(lastR = 1, lastR, lastR + 1)).Resize(UBound(arrRez, 1), _
                                                UBound(arrRez, 2) + 1).Value = arrRez
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2021-10-15
    相关资源
    最近更新 更多