【问题标题】:How to copy the whole txt file to Excel如何将整个txt文件复制到Excel
【发布时间】:2021-05-25 13:06:04
【问题描述】:

我的代码不能完全正常工作:

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 文件的所有内容粘贴到活动工作簿工作表“BOM”的单元格“C1”

按照我的建议修复代码:

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”定义为粘贴所有内容的范围

【问题讨论】:

  • 请注意,"doesn't work" 是一个非常无用的错误描述。你得到了哪个错误,我们需要的信息在哪里,或者你的代码做了什么而不是你期望它做什么。

标签: excel vba copy-paste txt


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢您的回答,这是一段有用的代码
  • 如果对您有所帮助,请让答案变得有用。
【解决方案2】:

问题是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 复制了整个工作表的所有 单元格。因此,如果您从 A1 开始,它们只能适合您粘贴的工作表,否则您将超出工作表的边界。解决方案不是复制所有单元格,而是只复制您需要的单元格。

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

【讨论】:

  • 更新了主帖
  • 这是有道理的,但是没有回答如何在我需要的范围内获取整个 txt 的问题。我刚才确实只是通过将 .cells 更改为 .UsedRange
  • @Eduards 好吧,我无法回答更详细的问题,因为我无法知道您的文件在 Excel 中打开后的样子。 • 如果这回答了您的问题,请点赞/标记为已解决:Accepting Answers: How does it work?
  • @Eduards 我编辑了答案,因此您可以使用它来将其标记为已解决。
  • @Eduards 这是一个新问题,所以请提出一个新问题,包括对问题的正确描述,最好是minimal reproducible example 以重现该问题。请注意,我们无法正确回答 cmets 中的问题。
【解决方案3】:

我经常将 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多