【问题标题】:How to open a txt file with vba code and and copy its contents to excel?如何使用 vba 代码打开 txt 文件并将其内容复制到 excel?
【发布时间】:2017-10-09 08:38:19
【问题描述】:

我需要从同一个文件夹打开多个 txt 文件并将其内容复制到单个 excel 文件(如模板)以修改值,然后我需要将修改后的值从 excel 复制到 txt 文件。我们如何做这个 VBA 自动化?任何答复都会有所帮助。

【问题讨论】:

  • 您能提供更多信息吗? txt 文件中的数据是什么样的?您是否需要将 txt 文件的内容放入特定的单元格中?您需要将哪些单元格放回 txt 文件中?
  • @Teasel 感谢您的关注。我需要将 txt 文件的内容从第 4 行复制到 excel 文件的第 1 张(注视单元格:A4),我有一些公式可以修改第 1 行的数据在第二张纸上。然后将第二张表中的修改数据复制并粘贴到同一个 txt 文件的第 4 行,然后保存。如果您有时间,请查看它。
  • 我写了一个应该有效的答案。如果是这种情况,请接受答案,那就太好了:)
  • @Teasel 感谢您的宝贵回复。它适用于一个文件。对于同一文件夹中的多个文件以进行重复操作,我应该在那里添加什么?你有什么想法吗?还有一个,我需要从第 4 行复制 txt 文件的内容,然后将其粘贴到 excel 文件中,盯着单元格 A4?
  • 这篇帖子stackoverflow.com/a/45749626/5836929的答案会给你一个很好的引导(这是你需要的,你在循环中调用你的方法)

标签: excel vba


【解决方案1】:

您可以使用以下方法打开文件(found there,适应它!)

Sub OpenAndImportTxtFile()
    Dim wbI As Workbook, wbO As Workbook
    Dim wsI As Worksheet

    Set wbI = ThisWorkbook
    Set wsI = wbI.Sheets("Sheet1") '<~~ Sheet where you want to import

    Set wbO = Workbooks.Open("path and name of your file")

    wbO.Sheets(1).Cells.Copy wsI.Cells

    wbO.Close SaveChanges:=False
End Sub

并使用以下方法导出您的工作表(found there

Sub SaveFile()
Dim ans As Long
Dim sSaveAsFilePath As String

On Error GoTo ErrHandler:

sSaveAsFilePath = "path and name of your file"

If Dir(sSaveAsFilePath) <> "" Then
    ans = MsgBox("File " & sSaveAsFilePath & " exists.  Overwrite?", vbYesNo + vbExclamation)
    If ans <> vbYes Then
        Exit Sub
    Else
        Kill sSaveAsFilePath
    End If
End If

Sheet1.Copy '//Copy sheet to new workbook
ActiveWorkbook.SaveAs sSaveAsFilePath, xlTextWindows '//Save as text (tab delimited) file

If ActiveWorkbook.Name <> ThisWorkbook.Name Then '//Double sure we don't close this workbook
    ActiveWorkbook.Close False
End If

My_Exit:
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume My_Exit
End Sub

打电话给他们

OpenAndImportTxtFile
SaveFile

【讨论】:

    【解决方案2】:

    有很多很好的指南,我曾经和你一样做同样的工作。

    对于 Excel 文本:

    http://www.excel-easy.com/vba/examples/read-data-from-text-file.html

    vba: Importing text file into excel sheet

    Excel转文本:

    http://www.excel-easy.com/vba/examples/write-data-to-text-file.html

    祝你好运

    【讨论】:

    • 感谢您的回复。
    【解决方案3】:

    听起来您想将所有文本文件合并到一个文件中。这个选项怎么样?

    Sub CombineTextFiles()
        Dim lFile As Long
        Dim sFile As String
        Dim vNewFile As Variant
        Dim sPath As String
        Dim sTxt As String
        Dim sLine As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            If .Show Then
                sPath = .SelectedItems(1)
                If Right(sPath, 1) <> Application.PathSeparator Then
                    sPath = sPath & Application.PathSeparator
                End If
            Else
                 'Path cancelled, exit
                Exit Sub
            End If
        End With
        vNewFile = Application.GetSaveAsFilename("CombinedFile.txt", "Text files (*.txt), *.txt", , "Please enter the combined filename.")
        If TypeName(vNewFile) = "Boolean" Then Exit Sub
        sFile = Dir(sPath & "*.txt")
        Do While Len(sFile) > 0
            lFile = FreeFile
            Open CStr(sFile) For Input As #lFile
            Do Until EOF(lFile)
                Line Input #1, sLine
                sTxt = sTxt & vbNewLine & sLine
            Loop
            Close lFile
            sFile = Dir()
        Loop
        lFile = FreeFile
        Open CStr(vNewFile) For Output As #lFile
        Print #lFile, sTxt
        Close lFile
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      相关资源
      最近更新 更多