将多个部分和子数据行从文本导入特定列

时间:2018-03-14 19:57:34

标签: excel vba

发现这一点,它有所帮助,但确实得到了我所需要的复杂性。  How to import specific text from files in to excel?

我正在做的是使用另一个脚本扫描一系列IP,这将为每个服务器的硬件配置文件创建一个文本文件。该脚本不是问题,并且此时正常工作。

然后我想要一个循环遍历上面创建的所有文件的脚本,并只将特定数据导入Excel电子表格。

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

我将该文件导入Excel的方式与此类似:

excel clip

这是一个包含大量部分的长文件的小剪辑。此剪辑显示2个部分的剪辑。我需要这个vba脚本才能确定[]部分,然后在其下拉一条特定的行。并且有许多部分可能几乎相同,例如[InstanceID: DIMM.Socket.A1][InstanceID: DIMM.Socket.A2],并且许多子行可能也是相同的,例如模型可能列在许多不同类型的组件下。

此脚本如何通过[InstanceID: DIMM.Socket.A1]下的模型并从中提取模型,但跳过所有其余类似部分。然后从[InstanceID: System.Embedded.1]部分拉出BIOSReleaseDate和BIOSVersionString行。我假设我可以使用原始脚本的其余部分将这些数据点放入特定列,例如Then col = 1Then col = A

谢谢!

0 个答案:

没有答案