簡體   English   中英

如何將整個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 文件的所有內容粘貼到活動工作簿表“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”定義為粘貼所有內容的范圍

此代碼將遍歷文件夾中的所有文本文件,並將一個文件導入一個單元格,然后將另一個文件導入另一個單元格,依此類推。

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

如果您只有一個文件需要導入,它只會導入那個文件。

問題是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 文件導入到我的工作簿中,以下代碼可能會對您有所幫助:

            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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM