比较Excel电子表格值与每行文本文件并根据结果返回输出

我需要将电子表格中的值与包含区域传输输出的文本文件进行比较,以根据电子表格中列出的types获取特定接口的IP地址。

  • 我的意见:

    • Excel电子表格:
       + ---------- + ------------ + |  名称 |  Inttypes |  + ---------- + ------------ + |  Switch-1 |  |  |  SERVER1 | 生产|  |  SERVER2 |  OOB |  |  Switch-2 |  |  |  SERVER3 | 生产|  |  SERVER4 | 其他|  + ---------- + ------------ + 
    • 文本文件:
       server1-prod.fqdn.com 86400 IN A 192.168.0.10
       server1-oob.fqdn.com 600 IN A 192.168.0.11
       server2-prod.fqdn.com 600 IN A 192.168.0.12
       server2-oob.fqdn.com 600 IN A 192.168.0.13
       server3-prod.fqdn.com 300 IN A 192.168.0.14
       server3-oob.fqdn.com 600 IN A 192.168.0.15
       server4-foo.fqdn.com 86400 IN A 192.168.0.16
      
  • 我期望的输出:

    交换机名称:Switch-1
    接口名称:server1-prod.fqdn.com  -  IP地址:192.168.0.10
    接口名称:server2-oob.fqdn.com  -  IP地址:192.168.0.11
    交换机名称:Switch-2
    接口名称:server3-prod.fqdn.com  -  IP地址:192.168.0.12
     server4-foo.fqdn.com未知的接口types:其他。
    

我目前的代码:

Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open("C:\Server-List-by-Switch.xlsx") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("FQDN-and-IP.txt") strNextLine = objFile.Readline arrList = Split(objFile.ReadLIne(),vbCrLf) intRow = 2 objFile.Close Do Until objExcel.Cells(intRow,1).Value = "" objHostName = objExcel.Cells(intRow,1).Value objIntfType = objExcel.Cells(intRow,2).Value If InStr(1,objExcel.Cells(intRow,1).Value,"Switch-",1) > 0 Then Wscript.Echo vbCrLF & "Switch Name: " & objHostName ElseIf InStr(1,objExcel.Cells(intRow, 1).Value,"Switch-",1) = 0 And InStr(1,objExcel.Cells(intRow,2).Value,"Production",1) > 0 then For Each line In arrList objFQDN = LCase(objHostName & "-prod.fqdn.com.") strFQDN = splitRE (strNextLine, "\s+") Wscript.Echo strFQDN(0) If InStr(Lcase(line),objFQDN) > 0 Then strIPAddress = Right(line, Len(line) - InStrRev(line," ") ) Wscript.Echo "Interface name(Production): " & strFQDN & " - IP Address: " & strIPAddress End If Next ElseIf InStr(1,objExcel.Cells(intRow, 1).Value,"Switch-",1) = 0 And InStr(1,objExcel.Cells(intRow,2).Value,"OOB",1) > 0 then For Each line In arrList objFQDN = LCase(objHostName & "-oob.fqdn.com.") strFQDN = splitRE (strNextLine, "\s+") Wscript.Echo strFQDN(0) If InStr(Lcase(line),objFQDN) > 0 Then strIPAddress = Right(line, Len(line) - InStrRev(line," ") ) Wscript.Echo "Interface name(Production): " & strFQDN & " - IP Address: " & strIPAddress End If Next Else Wscript.Echo objHostName & " has an unknown interface type: " & objIntfType End If intRow = intRow + 1 Loop Function splitRE(strSource, pattern) With CreateObject("vbscript.regexp") .Global = True .Pattern =pattern splitRE = Split(.Replace(strSource, " "), " ") End With End Function objExcel.Quit 

现在唯一的问题是strFQDNvariables只是重复地填充文本文件的第一行,所以比较失败:

server1-prod.fqdn.com。

你的代码很复杂,没有明显的原因。 例如,为什么macros打开工作簿“C:\ Server-List-by-Switch.xlsx”,而不是坐在?

我不知道Wscript.Echo是如何使用Debug.Print输出到立即窗口的。 我不是在每个variables的开头都有一个三字符types标识符的粉丝。 但是,如果使用,他们应该是正确的。 所以我认为objFile是可以的,因为它是一个对象,但objHostName是错误的,因为它是一个string。

我简化了你的代码,但保持了结构。 这意味着这段代码不能创build输出行server4-foo.fqdn.com Unknown interface type: Other. 因为你的代码validationExcel表格而不是文本文件。 代码可以重新sorting,但是您需要更清楚地了解数据的性质。

我在工作簿中有一个工作表“按服务器列表”。 它包含:

  | A | B | 1 | Name | Intf type | 2 | Switch-1 | | 3 | SERVER1 | Production| 4 | SERVER2 | OOB | 5 | Switch-2 | | 6 | SERVER3 | Production| 7 | SERVER4 | Other | 

macros的输出是:

 Switch Name: Switch-1 Interface name: server1-prod.fqdn.com - IP Address: 192.168.0.10 Interface name: server2-oob.fqdn.com - IP Address: 192.168.0.13 Switch Name: Switch-2 Interface name: server3-prod.fqdn.com - IP Address: 192.168.0.14 SERVER4 has an unknown interface type: Other 

这是我可以得到没有重新sorting的代码。

 Option Explicit Sub GenIPAddressReport() Dim arrList() As String Dim FQDN As String Dim FQDNPart() As String Dim HostName As String Dim IntfType As String Dim intRow As Long Dim IPAddress As String Dim Line As Variant Dim LinePart() As String Dim objFile As Object Dim objFSO As Object Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(ActiveWorkbook.Path & "\FQDN-and-IP.txt") arrList = Split(objFile.ReadAll(), vbCrLf) objFile.Close Set objFile = Nothing 'For Each Line In arrList ' Debug.Print Line 'Next intRow = 2 With Worksheets("Server-List-by-Switch") Do Until .Cells(intRow, 1).Value = "" HostName = .Cells(intRow, 1).Value IntfType = .Cells(intRow, 2).Value If InStr(1, HostName, "Switch-") > 0 Then Debug.Print "Switch Name: " & HostName ElseIf InStr(1, HostName, "Switch-") = 0 And _ InStr(1, IntfType, "Production") > 0 Then For Each Line In arrList FQDN = LCase(HostName & "-prod.fqdn.com") LinePart = Split(LCase(Line), " ") If FQDN = LinePart(0) Then Debug.Print "Interface name: " & LinePart(0) & _ " - IP Address: " & LinePart(UBound(LinePart)) Exit For End If Next ElseIf InStr(1, HostName, "Switch-") = 0 And _ InStr(1, IntfType, "OOB") > 0 Then For Each Line In arrList FQDN = LCase(HostName & "-oob.fqdn.com") LinePart = Split(LCase(Line), " ") If FQDN = LinePart(0) Then Debug.Print "Interface name: " & LinePart(0) & _ " - IP Address: " & LinePart(UBound(LinePart)) Exit For End If Next Else Debug.Print HostName & " has an unknown interface type: " & IntfType End If intRow = intRow + 1 Loop End With End Sub 

只是一个疯狂的猜测 – 两个都取代

strFQDN = splitRE (strNextLine, "\s+")

strFQDN = splitRE (line, "\s+")


或用其他行更新strNextLine ,现在它总是第一行..