2016-03-06 118 views
0

我通过@Scott Holtzman发现了这段代码,我需要稍微调整一下以符合我的需求。该代码将每行放在一个文本文件中,并将其放入Excel工作表(A1,B1,C1等)中的单独列中,每个文本文件存储在一个单独的行(1,2,3等)中。首先,我希望它只将文本放入Excel表格中,如果行以特定文本开始,则我希望它仅将每行中的一些文本复制到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中。认为这是因为文本文档中的每一行都有一个单元格 - 即使没有写入任何内容。

+0

所以,我正确理解您的文本文件中包含17行(“A1”,“A2”类型的14)...?如果不是,你能否提供更准确的信息,说明你的文件是什么样的?我是否正确理解您实际上想要为每个文件填充一个Excel行,其中每列对应于文件中的一行? – trincot

+0

尝试根据您的评论更新问题,是的,您理解正确,第1行中的文本文件A中​​包含A1,B1,C1等中的行,第2行中的文本文件B中包含A2,B2,C2中的行等等。 – Einar

回答

0

这里被填充在每个文件中的Excel工作表一行,解决方案开始在第2行。您应该手动填写职称即第一行如下:

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:”行,则会将工作表中的相应单元格留空,而不是将“答13:“在那里,导致转变。

即使这些行的顺序会改变,并且“From:”会出现在“Date:”之后,但这不会对输出产生负面影响。 “From”值始终会进入第一列,第二列中的“Date”值等。

另外,如果您的文件包含许多其他格式不同的行,则它们都将被忽略。

+0

谢谢Trincot,像一个魅力工作!非常好的“If key =”A“然后col = 2 + num”,在测试中我并不认为它是一个可能的问题,但通过观察,当然会有报道出现缺陷。 , 谢谢! – Einar

0

更换“你当的”身体与以下行

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 
+0

感谢Karpak,这解决了我之后只能写出超出所需文本的问题的一部分:但我仍然只需要复制所需行中的文本。 – Einar

+0

请使用上面的修改过的行,我希望它能正常工作。 – Karpak

+0

现在我们正在接近,不得不将线“TextLine = Trim ...”和“If Not ...”切换到位置,然后它将排除不感兴趣的行。现在是最后的挑战。脚本跳过的行仍然在excel中获取单元格,只是没有任何值的单元格。你知道如何避免这种情况,所以我只留下含有值的单元格吗? – Einar

相关问题