如何将整个 txt 文件复制到 Excel

How to copy the whole txt file to Excel

我有一个不完全有效的代码:

Sub Import_TXT()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook

    Application.GetOpenFilename ("Text Files (*.txt), *.txt")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1").Copy
        ThisWorkbook.Worksheets("BOM").Range("C1").PasteSpecial xlPasteValues
        OpenBook.colse False
    End If
End Sub

我正在尝试将 txt 文件的所有内容粘贴到活动工作簿的单元格“C1”sheet“BOM”

通过按照建议修复代码,我得到:

Sub Import_TXT()

Dim FileToOpen As Variant
Dim OpenBook As Workbook

FileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
OpenBook.Sheets(1).Cells.Copy
ThisWorkbook.Worksheets("BOM").Range("A1").PasteSpecial xlPasteValues
OpenBook.Close False
End If

End Sub

当我在“C1”中需要它时,它现在将所有内容粘贴到单元格“A1”中。更改时出现错误

ThisWorkbook.Worksheets("BOM").Range("A1").PasteSpecial xlPasteValues

ThisWorkbook.Worksheets("BOM").Range("C1").PasteSpecial xlPasteValues

说我只能粘贴“A1”

只需更改

.cells

.UsedRange

我可以将单元格“C1”定义为粘贴所有内容的范围

问题是 FileToOpen 始终是 False,因为在 Dim FileToOpen As Variant 之后您没有将任何值放入此变量。

你可能是故意的

FileToOpen = Application.GetOpenFilename ("Text Files (*.txt), *.txt")

另外 OpenBook.colse False 必须是 OpenBook.Close False(请参阅 colse 中的拼写错误)。


问题编辑后更新:

您收到该错误消息是因为您使用 OpenBook.Sheets(1).Cells.Copy 复制了整个 sheet 的 所有 个单元格。因此,如果您从 A1 开始,它们只能适合您粘贴的 sheet,否则您将超出 sheet 的边界。解决方案不是复制所有单元格,而是只复制您需要的单元格。

可能使用 OpenBook.Sheets(1).UsedRange.Copy 可以解决这个问题(取决于您的数据)。

我经常将 txt 文件导入我的工作簿,以下代码可能对您有所帮助:

            sub Copy_TXT_To_My_Plan()
            dim TextFile as integer
            dim FilePath as string
            dim FileContent as string
            FilePath = "C:/Files/MyExampleTextFile.txt" 'put the complete path to your textfile here

            TextFile = FreeFile
            Open FilePath for Input as TextFile
                    FileContent = Input(LOF(TextFile), TextFile)
            Close TextFile

            'now you have your txt in the FileContent string
            'you could just use Plan1.cells(1,1).Value = FileContent or
            'you could split it in a line array, something like:

            dim MyLineArray() as String
            MyLineArray = Split(FileContent, VbCrlf) 'or VbNewLine
            'then you can loop through the content of your file
            End Sub

此代码将遍历文件夹中的所有文本文件,并将一个文件导入一个单元格,然后将另一个文件导入另一个单元格,依此类推。

Sub Import_All_Text_Files()

    Application.ScreenUpdating = False

    Dim thisRow As Long
    Dim fileNum As Integer
    Dim strInput As String

    Const strPath As String = "C:\your_path_here\"  'Change as required
    Dim strFilename As String

    strFilename = Dir(strPath & "*.txt")

    With ActiveSheet
        thisRow = .Range("A" & .Rows.Count).End(xlUp).Row

        Do While strFilename <> ""

            fileNum = FreeFile()
            Open strPath & strFilename For Binary As #fileNum
            strInput = Space$(LOF(fileNum))
            Get #fileNum, , strInput
            Close #fileNum

            thisRow = thisRow + 1
            .Cells(thisRow, 1).Value = strInput

            strFilename = Dir
        Loop
    End With

    Application.ScreenUpdating = True

End Sub

如果您只有一个文件需要导入,它只会导入那个文件。