如何在VBA中仅提取特定字符串

时间:2015-07-27 08:57:47

标签: excel vba excel-vba

我已经在Extract strings from one text cell in VBA问了一个问题。首先,我为提出一个可能需要更多时间的具有挑战性的问题而道歉。

基本上它是一个从一个单元格中提取主机名和IP地址的宏,我必须从一列中的许多这样的单元格中提取。 @Nicolas已经帮我写了一个非常好的剧本。但是,由于输入数据的变化,如何格式化IP地址和主机名是非常不可预测的。作为旁注,该代码可以在@Nicolas的答案中找到。

现在我收到错误消息:

  

下标超出范围

tempIps = Trim(Split(lineList(line), ":")(1))行,因为有时输入数据如下所示,其中包含关键字但不具备我需要的真实主机名和IP地址。

  

主机名机架密钥IP地址应用程序名称

有时在For Index = 1 To UBound(hostList),因为找不到主机名或IP地址。

以下是一些供您参考的示例,如果需要,我可以提供更多:

示例1:

Please refer long description 22:00 to 01:00 AM

MW SA to draw password for the following servers.

Hostname : a01gibapp1a IP Address : 10.89.96.21 Privileged ID : root  

Hostname : a01gibweb1a IP Address : 10.89.75.23 Privileged ID : root  

01:00 to 03:00 AM

MW SA to run the script implementation.sh which is under /staginapp/jul2015/20jul15/ in all the IB app Servers.
MW SA to run the script implementation.sh which is under /tmp/CH64698/ in all the web servers.
MW SA to restart the WebSphere instances one by one in all the IB App servers given above.
MW SA to restart the IB IHS instances if required.

03:00 to 04:00
AMS technical verification
BU Live Verification.

示例2:

Please refer to long description & attachment Implementation steps:
=====================
Schedule time: 20th July 10PM to 21st July 3AM

Allow MW SA to draw the root password for below servers.
a01gibpns1a 10.89.71.26
a01gibpns2a 10.89.71.27
a01gibpns3a 10.89.71.34
a01gibpns4a 10.89.71.33

a01ribpns1a 10.89.35.83

这是我的代码的全部内容:

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
Dim X As Integer, wslasteow As Integer

With Sheets(1)

wSlastRow = .Rows(.Range("W:W").Rows.Count).End(xlUp).Row

For X = 4 To wSlastRow

hostIndex = 1
ipIndex = 1

'Get address string from cell
addressStream = .Range("W" & X)

'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
     If InStr(lineList(line), "IP Address") Or InStr(lineList(line), "IP ADDRESS") Or InStr(lineList(line), "IP Address ") 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:") Or InStr(lineList(line), "HostName:") Or 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
Sheets(2).Range("A" & (X)).Value = result

Next X
End With

End Sub

2 个答案:

答案 0 :(得分:1)

你能不能只检查字符串,看看你试图拆分字符串的字符是否实际上在你拆分之前在字符串中?

{}

如果没有要拆分的冒号,这将阻止代码尝试拆分字符串。

如果我错误地解释了你的内容,请告诉我。

答案 1 :(得分:1)

要从字符串中提取IP地址,请使用正则表达式。添加对Microsoft VBScript Regular Expressions 5.5库的引用,并使用RegExp对象根据模式测试字符串。例如,要从单元格A1中提取IP地址:

Dim re As New RegExp
re.Pattern = "(?:\d{1,3}\.){3}\d{1,3}"

If re.Test(Range("A1")) Then

    ' [A1] contains an IP address. Extract it...
    Debug.Print "IP address found = " & re.Execute(Range("A1"))(0)

End If