簡體   English   中英

如何從文件導入特定文本到Excel?

[英]How to import specific text from files in to excel?

我通過@Scott Holtzman找到了這段代碼,我需要對其進行一些調整以滿足我的需求。 此代碼將文本文件中的每一行放入Excel工作表(A1,B1,C1等)的單獨列中,每個文本文件存儲在單獨的行(1、2、3等)中。 首先,我希望它僅在行以特定文本開頭的情況下才將文本放入excel工作表,其次我希望它僅將每行中的某些文本復制到excel工作表中。

Sub ReadFilesIntoActiveSheet()

Dim fso As FileSystemObject
Dim folder As folder, file As file, FileText As TextStream
Dim TextLine As String, Items() As String
Dim i As Long, cl As Range

' Get a FileSystem object
Set fso = New FileSystemObject

' get the directory you want
Set folder = fso.GetFolder("D:\YourDirectory\")

Dim x As Long
x = 1 'to offset rows for each file

' Loop thru all files in the folder
For Each file In folder.Files

' set the starting point to write the data to
Set cl = ActiveSheet.Cells(x, 1)

' Open the file
Set FileText = file.OpenAsTextStream(ForReading)

Dim j As Long
j = 0 'to offset columsn for each line
' Read the file one line at a time
Do While Not FileText.AtEndOfStream

    TextLine = FileText.ReadLine 'read line

    cl.Offset(, j).Value = TextLine 'fill cell

    j = j + 1
Loop

' Clean up
FileText.Close

x = x + 1

Next file

Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing

End Sub

我的文本文件如下所示:

From:NameName           'want all text except the "FROM:"
Date:yyyy.mm.dd         'want all text except the "Date:"
Type: XXXXXXXXX         ' I don't want this line into excel
To: namename            ' I don't want this line into excel

----------------------------- xxxxxxx ---------------------
A1: Tnr xxxxxxxxxxxxx   'want all text except the "A1: Tnr" only next 13char
A2: texttext            'want all text except the "A2:"
An:                     'A1 and up to A14
A14: texttext           'want all text except the "A14:"  

------------------------------ xxxxxx ----------------------

因此,文本文件中總共有22行。

並且如果可以使用FROM:,DATE:,A1:到A14:作為第一行的標題,那將是史詩般的。

嘗試用谷歌搜索它,並嘗試了一下:

TextLine =    FileText.ReadLine 'read line
If InStr(TextLine, "A1:") 

但這僅適用於一行,而我似乎無法使它適用於多行。 此外,它將輸出放在單元格F1中,而不是A1中。 認為這是因為文本文檔中的每一行都有一個單元格-即使沒有寫入任何內容。

用以下幾行替換“ Do While's”主體

TextLine = FileText.ReadLine 'read line
If Not (Left(TextLine, 1) = "T" Or Left(TextLine, 1) = "-") Then
    TextLine = Trim(Mid(TextLine, InStr(TextLine, ":") + 1))
    If (TextLine <> "") Then
       cl.Offset(, j).Value = TextLine 'fill cell
       j = j + 1
    End If
End If

這是一個解決方案,它從第2行開始在每個文件的Excel工作表中填充一行。您應該按如下方式手動填寫第一行中的標題:

From | Date | A1 | A2 | ... | A14

將跳過您不感興趣的行,並將值放在正確的列中:

Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder, file As file, FileText As TextStream
    Dim TextLine As String
    Dim cl As Range

    Dim num As Long ' numerical part of key, as in "Ann:"
    Dim col As Long ' target column in Excel sheet
    Dim key As String ' Part before ":"
    Dim value As String ' Part after ":"

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' Get the directory you want
    Set folder = fso.GetFolder("D:\YourDirectory\")

    ' Set the starting point to write the data to
    ' Don't write in first row where titles are
    Set cl = ActiveSheet.Cells(2, 1)

    ' Loop thru all files in the folder
    For Each file In folder.Files
        ' Open the file
        Set FileText = file.OpenAsTextStream(ForReading)

        ' Read the file one line at a time
        Do While Not FileText.AtEndOfStream

            TextLine = FileText.ReadLine 'read line

            key = Split(TextLine & ":", ":")(0)
            value = Trim(Mid(TextLine, Len(key)+2))
            num = Val(Mid(key,2))
            If num Then key = Replace(key, num, "") ' Remove number from key
            col = 0
            If key = "From" Then col = 1
            If key = "Date" Then col = 2
            If key = "A"    Then col = 2 + num
            If col Then
                cl.Offset(, col-1).Value = value ' Fill cell
            End If
        Loop

        ' Clean up
        FileText.Close
        ' Next row
        Set cl = cl.Offset(1) 
    Next file
End Sub

即使文件中缺少項目,上面的代碼也可以很好地工作,例如,如果不存在帶有“ A12:”的行,則將工作表中的相應單元格留空,而不是將值“ A13:”在那里,引起了轉變。

即使行的順序發生變化,並且“發件人:”將出現在“日期:”之后,這也不會對輸出產生負面影響。 “ From”值將始終進入第一列,“ Date”值將進入第二列,依此類推。

同樣,如果您的文件包含許多其他格式不同的行,則它們都將被忽略。

暫無
暫無

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

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