4

我希望在 Excel 中编写一个函数,将前导零添加到构成 IP 地址的八位字节中:例如,在 172.19.1.17 中,我想更改 0.19。到 .019., .1. 到 .001.,最后的 .17 到 0.017。

Te Cells.Teplace 功能似乎不接受?作为通配符。另外,有没有一种方法可以表示“字符串结尾”,这样我就可以将前导零添加到最后一个八位字节,在上面的示例中为 0.17。

谢谢伊恩

Cells.Replace What:=".1?.", Replacement:=".01?.", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

这确实找到了“10”。“11。” “12。” 等,但将它们全部替换为“.01?”。

4

3 回答 3

3

作为替代方案,您可以使用此公式将零添加到 IP 部分(看起来很糟糕,但将所有部分分开处理,最后将它们混合起来):

=REPT(0,4-FIND(".",A1))&LEFT(A1,FIND(".",A1)-1)&"."&
REPT(0,4-FIND("@",SUBSTITUTE(A1,".","@",2))+FIND(".",A1))&MID(A1,FIND(".",A1)+1,FIND("@",SUBSTITUTE(A1,".","@",2))-FIND(".",A1)-1)&"."&
REPT(0,4-FIND("@",SUBSTITUTE(A1,".","@",3))+FIND("@",SUBSTITUTE(A1,".","@",2)))&MID(A1,FIND("@",SUBSTITUTE(A1,".","@",2))+1,FIND("@",SUBSTITUTE(A1,".","@",3))-FIND("@",SUBSTITUTE(A1,".","@",2))-1)&"."&
REPT(0,3-LEN(A1)+FIND("@",SUBSTITUTE(A1,".","@",3)))&RIGHT(A1,LEN(A1)-FIND("@",SUBSTITUTE(A1,".","@",3)))

无论换行符,您都可以将其粘贴到原样B1(假设您的 IP 在 A 列开始)。A1

示例文件:https ://www.dropbox.com/s/vun6urvukch9uvv/IPoctets.xlsx

于 2013-02-20T19:02:40.523 回答
1

你可以这样做:

请务必将 Application.UsedRange 替换为包含 IP 地址的实际范围

Sub PadIP()
    Dim Arr As Variant
    Dim ipAddr As String
    Dim vCell As Variant
    Dim n As Long

    'Replace ActiveSheet.UsedRange with the range containing your data
    '
    'If data is contained in column A and you have a column header
    'Example: Range(Cells(2, 1), Cells(ActiveSheet.UsedRange.Rows.Count, 1))
    For Each vCell In ActiveSheet.UsedRange
        Arr = Split(vCell.Value, ".")

        For n = 0 To UBound(Arr)
            If (n + 1) Mod 4 = 0 Then
                ipAddr = ipAddr & Right(String(3, "0") & Arr(n), 3)
            Else
                ipAddr = ipAddr & Right(String(3, "0") & Arr(n), 3) & "."
            End If
        Next
       vCell.Value = ipAddr
       ipAddr = ""
    Next
End Sub
于 2013-02-20T19:02:36.673 回答
1

我也可以玩:)?

这是我上面评论的进一步内容。这是一个关于如何找到.??.和制作它的例子.0??.

我假设数据可以工作表中的任何位置。

Sub Sample()
    Dim oRange As Range, aCell As Range, bCell As Range
    Dim ws As Worksheet
    Dim ExitLoop As Boolean
    Dim SearchString As String

    On Error GoTo Whoa

    Set ws = Worksheets("Sheet1")
    Set oRange = ws.Cells

    SearchString = ".??."

    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)

    If Not aCell Is Nothing Then
        Set bCell = aCell

        aCell.Value = CleanIt(aCell.Value)

        Do While ExitLoop = False
            Set aCell = oRange.FindNext(After:=aCell)

            If Not aCell Is Nothing Then
                If aCell.Address = bCell.Address Then Exit Do
                aCell.Value = CleanIt(aCell.Value)
            Else
                ExitLoop = True
            End If
        Loop
    End If
    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub

Function CleanIt(rng)
    Dim MyAr() As String
    Dim strTemp As String

    MyAr = Split(rng, ".")

    For i = LBound(MyAr) To UBound(MyAr)
        If Len(MyAr(i)) = 2 Then
            MyAr(i) = "0" & MyAr(i)
        End If
        strTemp = strTemp & "." & MyAr(i)
    Next i
    CleanIt = Mid(strTemp, 2)
End Function

截屏

在此处输入图像描述

注意:这只是用于演示目的的示例。上面的代码需要进行更多调整,以便它也可以处理其他场景。

于 2013-02-20T19:17:25.387 回答