2015-07-20 91 views
0

我要提取的主機名和IP地址,該地址是從一個文本單元Hostnames:IP Address:後的字符串如下所示,一個小區可以具有多個主機名和IP地址,以及我有許多細胞作爲這樣的:提取字符串VBA

這裏我的樣本數據:

Please refer to CR_Implementation_Reversion_Plan UNIX and Informatica (9.1) 
-------------------- 
IP Address: 10.89.140.123, 10.89.140.125, 10.89.140.127, 10.89.140.92 
Hostname: a01gbiapp1a, a01gbiapp1b, a01gbiapp1z, w01ggdwtd1a 
Informatica ID: proddeploy(Informatica) 
Unix ID:cbitimpl, root(su- bipadm,pdm, bipxtr, bdwadm, cpmsadm, dqadm, rmgadm, cbgadm, bdwxtr, hkb2adm, biptwadm, bipinadm, bipcnadm, biphkadm, bipidadm, bipotadm) 

UNIX and Informatica (7.1) 
-------------------- 
IP Address: 10.89.140.52, 10.89.140.53, 10.89.140.37, 10.89.140.37 
Hostname: a01ginf1a , a01ginf1b, a01ginf1z, a01ginf1z 
Informatica ID: proddeploy(Informatica) 
Unix ID: cbitimpl, root(su- bipadm, bipdm, bipxtr, bdwadm, cpmsadm, dqadm, rmgadm, cbgadm, bdwxtr, hkb2adm, biptwadm, bipinadm, bipcnadm, biphkadm, bipidadm, bipotadm) 

Teradata 
-------- 
IP Address: 10.89.140.97 
Hostname: r01gtddb1a 
ID: fdwadmin, bdwadmin 

InformaticaClient 
------------------ 
w01gdnv1a or w01ggdwtd1a or w01ggdwtd10a 10.89.140.92(Informatica Client), 
ID: infadeploy 

BO implementation:Draw Rack Keys: 
------------------------------------- 
IP Address: 10.89.140.105, 10.89.140.106, 10.89.140.48, 10.89.140.49 
Hostname: W01GBOXIAPP1A, W01GBOXIAPP2A, W01GBOXIAPP3A, W01GBOXIAPP4A 
BO ID: boimpl 


SAS 
--- 
IP Address: 10.89.136.122, 10.89.136.125, 10.89.136.126 
Hostname: w01gsaseapp1a, a01gsaseapp1a, a01gsaseapp2a 
SAS ID: sas_impl 

Power Exchange 
-------------- 
IP ADDRESS: 10.80.250.73 
ID: DMSDDTL1, DMSDDTL2, DMSDDTL3 

UI 
--- 
IP: 10.89.140.112 
Hostname: A01GWAWEB1A 
ID: wasuser 

WODM 
---- 
IP: 10.89.140.109 
Hostname: A01GWABRE1A 
ID: wodmadm 

DB2 Server 
---------- 
IP: 10.89.140.113 
DB : DBSWATS 
Schema : watsusr 


DBS_IBMSG_BTEAM: 
Pls allow Ramakishore M/ Prashanth Badugu/ Srinivasa Theerdhala Part B and  Satish Parmarthy/Dileep EP/Krishna Reddy/Raghavendra Goud Part A to withdraw the 
"infaoper" and "proddeploy" ID for server a01gbiapp1a, a01gbiapp1b, a01gbiapp1z. 

我已經使用Perl來提取HTML文件中的字符串是這樣的:

if ($ReadFile =~ /^OS Version\/Service Pack\:/)  { 
    #print "$ReadFile\n" 
    ($OSVer)=(split /:/, $ReadFile)[1]; print "$OSVer\n"; 
    &myServerInfo("$OSVer","4"); 
} 

我想知道是否可以使用相同的方法來提取VBA中的此類文本單元的主機名和IP地址,我們將不勝感激。

+0

究竟怎樣的文字出現在細胞?是否有換行符? –

+0

@AlexP \t 是的,它看起來和我在問題中上傳的截圖完全一樣,有很多換行符。 – excel

+0

說清楚,這一切文本是在一個單元還是多個單元?不清楚..! –

回答

0

在VBA中,這會將單元格放在右邊,例如如果上面是A1,A1選擇運行這個,然後在B1,C1,D1你將有 「A01 ...」, 「... 10.89」, 「swnet」

Sub t() 
With ActiveCell 
    sp = Split(.Value, Chr(10)) 

    For i = 0 To UBound(sp) 
     sp2 = Split(sp(i), ":") 

     .Offset(0, 1 + i).Value = Trim(sp2(1)) 
    Next 
End With 
End Sub 

能否改變activecell到然後循環上面的代碼,以一次複製更多的tahn一行

3

首先,我想告訴我的代碼可以完美處理的輸入數據。在您的輸入數據中,「IP地址」和「主機名」的節奏必須相同。我是指那些對如下靜態序列:

................................
- --------------------------
IP地址:
主機名:
................ ................
---------------------------
IP地址:
主機名:
................................
--------------- ------------
IP:
主機名:
............................... 。
---------------------------
IP Address:
................ ........ ........
---------------------------
IP地址:
主機名:

的輸入數據必須是該串行格式。只有在這種格式下,我的代碼才能正常工作。好..?我的意思是它不能用於以下串行輸入:

................................
---------------------------
IP Address:
Hostname:
............... .................
---------------------------
IP地址:
主機名:
................................
---------------------------
主機名:
IP地址:

所以,我做了小的修改,以您的輸入數據如下:

Please refer to CR_Implementation_Reversion_Plan UNIX and Informatica (9.1) 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.123, 10.89.140.125, 10.89.140.127, 10.89.140.92 
Hostname: a01gbiapp1a, a01gbiapp1b, a01gbiapp1z, w01ggdwtd1a 
Informatica ID: proddeploy(Informatica) 
Unix ID:cbitimpl, root(su- bipadm,pdm, bipxtr, bdwadm, cpmsadm, dqadm, rmgadm, cbgadm, 
     bdwxtr, hkb2adm, biptwadm, bipinadm, bipcnadm, biphkadm, bipidadm, bipotadm) 


UNIX and Informatica (7.1) 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.52, 10.89.140.53, 10.89.140.37, 10.89.140.37 
Hostname: a01ginf1a , a01ginf1b, a01ginf1z, a01ginf1z 
Informatica ID: proddeploy(Informatica) 
Unix ID: cbitimpl, root(su- bipadm, bipdm, bipxtr, bdwadm, cpmsadm, dqadm, rmgadm, 
     cbgadm, bdwxtr, hkb2adm, biptwadm, bipinadm, bipcnadm, biphkadm, bipidadm, 
    bipotadm) 


Teradata 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.97 
Hostname: r01gtddb1a 
ID: fdwadmin, bdwadmin 


InformaticaClient 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.92 
Hostname: w01gdnv1a or w01ggdwtd1a or w01ggdwtd10a 
ID: infadeploy(Informatica Client) 


BO implementation:Draw Rack Keys: 
--------------------------------------------------------------------------------- 
IP: 10.89.140.105, 10.89.140.106, 10.89.140.48, 10.89.140.49 
Hostname: W01GBOXIAPP1A, W01GBOXIAPP2A, W01GBOXIAPP3A, W01GBOXIAPP4A 
BO ID: boimpl 


SAS 
--------------------------------------------------------------------------------- 
IP ADDRESS: 10.89.136.122, 10.89.136.125, 10.89.136.126 
Hostname: w01gsaseapp1a, a01gsaseapp1a, a01gsaseapp2a 
SAS ID: sas_impl 


Power Exchange 
--------------------------------------------------------------------------------- 
IP Address: 10.80.250.73 
ID: DMSDDTL1, DMSDDTL2, DMSDDTL3 


UI 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.112 
Hostname: A01GWAWEB1A 
ID: wasuser 


WODM 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.109 
Hostname: A01GWABRE1A 
ID: wodmadm 


DB2 Server 
--------------------------------------------------------------------------------- 
IP Address: 10.89.140.113 
DB : DBSWATS 
Schema : watsusr 


DBS_IBMSG_BTEAM: 
Pls allow Ramakishore M/ Prashanth Badugu/ Srinivasa Theerdhala Part B and  
Satish Parmarthy/Dileep EP/Krishna Reddy/Raghavendra Goud Part A to withdraw 
the "infaoper" and "proddeploy" ID for server a01gbiapp1a, a01gbiapp1b, a01gbiapp1z. 

我修改我的運行代碼,輸入如下:

Public Sub splitHostnameAndIPAddress() 

    Dim addressStream As String 

    Dim lineList() As String 
    Dim line As Integer 
    Dim tempHosts, tempIps As String 
    Dim hostList(), ipList() As String 
    Dim hostIndex, ipIndex, tempIndex As Integer 
    Dim result As String 
    Dim ipFlag As Boolean 

    hostIndex = 1 
    ipIndex = 1 

    'Get address string from cell 
    addressStream = Sheets("Sheet5").Range("A1") 

    'Split by vbLf(line by line) 
    lineList = Split(addressStream, vbLf) 

    'Loop all line 
    For line = 0 To UBound(lineList) 

     'If "IP Address" string include in line, store ip address 
     If InStr(lineList(line), "IP Address") Or InStr(lineList(line), "IP ADDRESS") Or InStr(lineList(line), "IP") Then 

      'Check for getting right pair. 
      If ipFlag Then 
       hostIndex = hostIndex + 1 
      Else 
       ipFlag = True 
      End If 

      'Getting Ip(s) 
      tempIps = Trim(Split(lineList(line), ":")(1)) 

      'If there is several ip in string which are separated by "," 
      If InStr(tempIps, ",") Then 

       'Loop ip list which is separated by "," and store 
       For tempIndex = 0 To UBound(Split(tempIps, ",")) 

        ReDim Preserve ipList(ipIndex) 

        ipList(ipIndex) = Trim(Split(tempIps, ",")(tempIndex)) 

        ipIndex = ipIndex + 1 

       Next tempIndex 

      'Else single ip is store 
      Else 

       ReDim Preserve ipList(ipIndex) 

       ipList(ipIndex) = tempIps 

       ipIndex = ipIndex + 1 

      End If 


     'If "Hostnames" string include in line, store host name 
     ElseIf InStr(lineList(line), "Hostname") Then 

      'Check for getting right pair. 
      If ipFlag Then 
       ipFlag = False 
      Else 
       ipIndex = ipIndex + 1 
      End If 

      'Getting host(s) 
      tempHosts = Trim(Split(lineList(line), ":")(1)) 

      'If there is several host in string which are separated by "," 
      If InStr(tempHosts, ",") Then 

       'Loop host list which is separated by "," and store 
       For tempIndex = 0 To UBound(Split(tempHosts, ",")) 

        ReDim Preserve hostList(hostIndex) 

        hostList(hostIndex) = Trim(Split(tempHosts, ",")(tempIndex)) 

        hostIndex = hostIndex + 1 

       Next tempIndex 

      'Else single host is store 
      Else 

       ReDim Preserve hostList(hostIndex) 

       hostList(hostIndex) = tempHosts 

       hostIndex = hostIndex + 1 

      End If 

     End If 

    Next line 

    'Adjust two list size 
    If hostIndex > ipIndex Then 
     ReDim Preserve ipList(hostIndex - 1) 
    ElseIf ipIndex > hostIndex Then 
     ReDim Preserve hostList(ipIndex - 1) 
    End If 

    'Loop host list 
    For index = 1 To UBound(hostList) 

     'Add host & ip pair 
     result = result & ipList(index) & vbTab & hostList(index) & vbNewLine 

    Next index 

    'Show result 
    MsgBox result 

End Sub 

然後,我測試了我的代碼,我得到了以下的結果。

enter image description here

我嘗試我能爲這個問題。我再也想不到下一個輸入數據了。所以,根據我的建議嘗試調整輸入數據。如果可以,您可以修改我的代碼以符合您的要求。感謝您提出具有挑戰性的問題。

+0

嗨,感謝您的幫助,但是當我運行這段代碼時,在'result = result&hostList(Index)&vbTab&ipList(Index)&vbNewLine'上出現'Subscript out或range'錯誤,宏不能找到IP地址 – excel

+0

其實,你給我們提供了錯誤的信息。但是,別擔心,我得到了你。我修改了我的代碼。試試吧。小心下次發佈問題。 –

+0

非常感謝您修改您的答案,但它仍然給我同樣的錯誤,問題與'ipList(Index)' – excel

0

這得到了大部分。它假定數據位於活動工作表的單元格A1中。它可以很容易地修改爲在其他單元上工作。讓我知道你是否需要這個。

Public Sub ParseHostIP() 

    Dim i As Long 
    Dim s As String, sIP As String, sHost As String 
    Dim v As Variant, vIP As Variant, vHost As Variant 

    s = [a1] 
    s = Replace(Replace(Replace(s, "IP Address:", "~~"), "IP ADDRESS:", "~~"), "IP:", "~~") 
    s = Replace(Replace(s, "Hostname:", "~~"), " ", vbNullString)  
    v = Split(s, vbLf) 
    For i = 0 To UBound(v) 
     If Left$(v(i), 2) = "~~" Then 
      'Stop 
      If Left$(v(i + 1), 2) = "~~" Then 
       sIP = sIP & "," & Mid$(v(i), 3) 
       sHost = sHost & "," & Mid$(v(i + 1), 3) 
       i = i + 1 
      End If 
     End If 
    Next 
    vIP = Split(Mid$(sIP, 2), ",") 
    vHost = Split(Mid$(sHost, 2), ",") 


    '******************************************* 
    ' Done. 
    ' The rest is here only to display results. 
    '******************************************* 
    s = vbNullString 
    For i = 0 To UBound(vIP) 
     s = s & vIP(i) & vbTab & vHost(i) & vbLf 
    Next 
    MsgBox s 

End Sub 

更新#1

基礎上的評論這裏提供新的信息是一個修訂版:

Public Sub ParseHostIP() 

    Dim i As Long, k As Long 
    Dim s As String, sIP As String, sHost As String 
    Dim vIn As Variant, vOut As Variant, v As Variant   

    With Worksheets(1).Columns(2).Cells(4) 
     vIn = .Resize(.End(xlDown).Row).Value 
    End With 

    ReDim vOut(1 To UBound(vIn), 1 To 2) 
    For k = 1 To UBound(vIn) 
     s = vIn(k, 1) 
     s = Replace(Replace(Replace(s, "IP Address:", "~~"), "IP ADDRESS:", "~~"), "IP:", "~~") 
     s = Replace(Replace(s, "Hostname:", "~~"), " ", vbNullString) 

     v = Split(s, vbLf) 

     For i = 0 To UBound(v) 
      If Left$(v(i), 2) = "~~" Then 
       'Stop 
       If Left$(v(i + 1), 2) = "~~" Then 
        sIP = sIP & "," & Mid$(v(i), 3) 
        sHost = sHost & "," & Mid$(v(i + 1), 3) 
        i = i + 1 
       End If 
      End If 
     Next 

     vOut(k, 1) = Mid$(sIP, 2) 
     vOut(k, 2) = Mid$(sHost, 2) 
    Next 

    Worksheets(2).Range("a1").Resize(UBound(vOut), 2) = vOut 

End Sub 
+0

這項工作是爲了您? –

+0

嗨我測試它,感謝您的幫助,將盡快回復^^ – excel

+0

嗨,實際上所有的數據都在第2列從第4行開始,直到最後一行有數據,我需要粘貼結果(2).Range(「A1」)。Value = result' – excel