我制作了一个函数,可以将字符串输入转换为您要求的格式。
它使用Split函数从起始和结束范围比较ip地址的每一部分。
Function ipFormatter(ByVal ipRange As String) As String
'Skip inputs with no "-"
If InStr(1,ipRange,"-") = 0 Then
ipFormatter = ipRange
Exit Function
End If
'Removing all spaces
ipRange = Replace(ipRange," ","")
'Splitting the ip addresses into arrays of strings
Dim ipStart() As String,ipEnd() As String
ipStart = Split(Split(ipRange,"-")(0),".")
ipEnd = Split(Split(ipRange,"-")(1),".")
Dim ipOut() As String
ReDim ipOut(UBound(ipStart))
Dim i As Long,setRange As Boolean
For i = LBound(ipStart) To UBound(ipStart)
If setRange Then
'After a range
ipOut(i) = "*"
ElseIf ipStart(i) = ipEnd(i) Then
'Exact Match
ipOut(i) = ipStart(i)
Else
'IP Range found
ipOut(i) = ipStart(i) & "-" & ipEnd(i)
setRange = True 'All values after this one will be "*"
End If
Next i
ipFormatter = Join(ipOut,".")
End Function
以下是如何使用该功能的示例
Sub test()
Const inputstr As String = "123.45.678.0 - 123.45.679.255"
MsgBox ipFormatter(inputstr)
End Sub
Sub example()
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Sheet1")
Dim lastrow As Long
lastrow = Sht.Rows(Sht.Rows.Count).Cells(1).End(xlUp).Row
Dim i As Long
For i = 1 To lastrow
Sht.Cells(i,2) = ipFormatter(Sht.Cells(i,1))
Next i
End Sub
本文链接:https://www.f2er.com/127414.html