跳到主要内容

如何在Excel中快速将IP地址从低到高排序?

通常,我们在Excel中使用Sort函数对字符串进行排序。 但是,如果需要一些IP地址进行排序,则直接使用“排序”功能可能会导致排序顺序错误,如下面的屏幕截图所示。 现在,我有一些方法可以在Excel中快速正确地对IP地址进行排序。

排序功能排序错误 正确排序
doc排序ip 1 doc排序ip 2

按公式对IP地址进行排序

按VBA排序IP地址

按文本到列对IP地址排序


按公式对IP地址进行排序

使用公式填充IP地址,然后排序。

1.选择一个与IP地址相邻的单元格,然后键入此公式

=TEXT(LEFT(A1,FIND(".",A1,1)-1),"000") & "." & TEXT(MID(A1,FIND( ".",A1,1)+1,FIND(".",A1,FIND(".",A1,1)+1)-FIND(".",A1,1)-1),"000") & "." & TEXT(MID(A1,FIND(".",A1,FIND(".",A1,1)+1)+1,FIND(".",A1, FIND(".",A1,FIND(".",A1,1)+1)+1)-FIND(".",A1,FIND(".",A1,1)+1)-1), "000") & "." & TEXT(RIGHT(A1,LEN(A1)-FIND(".",A1,FIND(".",A1,FIND( ".",A1,1)+1)+1)),"000")

express 输入 键,然后将填充手柄拖动到单元格上以应用此公式。
doc排序ip 3

2.复制公式结果并将其作为值粘贴在下一列中。 看截图:

doc排序ip 4
doc排序ip 5

3.保持粘贴值处于选中状态,然后单击 时间 > 将A到Z排序.
doc排序ip 6

4.在 排序警告 对话,保持 扩大选择 检查。
doc排序ip 7

5。 单击 排序。 现在,IP地址已从低到高排序。
doc排序ip 2

您可以删除帮助器列。


按VBA排序IP地址

这是一个VBA代码,也可以为您提供帮助。

1。 按 Alt + F11键 启用 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,将代码复制并粘贴到空白脚本中。

VBA:填写IP地址

Sub FormatIP()
'UpdatbyExtendoffice20171215
    Dim xReg As New RegExp
    Dim xMatches As MatchCollection
    Dim xMatch As Match
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim xArr() As String
    On Error Resume Next
    Set xRg = Application.InputBox("Select cells:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    With xReg
        .Global = True
        .Pattern = "\d{1,3}\.+\d{1,3}\.+\d{1,3}\.+\d{1,3}"
        For Each xCell In xRg
            Set xMatches = .Execute(xCell.Value)
            If xMatches.Count = 0 Then GoTo xBreak
            For Each xMatch In xMatches
                xArr = Split(xMatch, ".")
                For I = 0 To UBound(xArr)
                    xArr(I) = Right("000" & xArr(I), 3)
                    If I <> UBound(xArr) Then
                        xArr(I) = xArr(I) & "."
                    End If
                Next
            Next
            xCell.Value = Join(xArr, "")
xBreak:
        Next
    End With
End Sub

doc排序ip 8

3。 然后点击 工具 > 参考文献,并检查 Microsoft VBScript正则表达式5.5 在弹出的对话框中。

doc排序ip 9
doc排序ip 10

4。 点击 OK 并按下 F5 键,会弹出一个对话框,提醒您选择一个工作范围。
doc排序ip 11

5。 点击 OK。 然后,IP地址已填充为零。

6.选择IP地址,然后单击 时间 > 将A到Z排序 对它们进行排序。


按文本到列对IP地址排序

实际上,“文本到列”功能也可以在Excel中帮您一个忙。

1.选择您使用的单元格,然后单击 时间 > 文本到列。 看截图:
doc排序ip 12

2.在 将文本转换为列向导 对话框,请执行以下操作:

单向阀 分隔和点击 下一页;

单向阀 其他名称 和类型 . 进入文本框,然后单击 下一页;

选择IP地址旁边的单元格以放置结果。 请点击 完成.

doc排序ip 13
doc排序ip 14
doc排序ip 15

3.选择所有包含IP地址和拆分单元格的单元格,然后单击 时间 > 排序.
doc排序ip 16

4.在 排序 对话框,单击 添加等级 将数据从B列排序到E(拆分的单元格)。 看截图:
doc排序ip 17

5。 点击 OK。 现在,列已排序。
doc排序ip 18


最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Traducido al español:
=TEXTO(IZQUIERDA(A1,ENCONTRAR(".",A1,1)-1),"000") & "." & TEXTO(EXTRAE(A1,ENCONTRAR( ".",A1,1)+1,ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)-ENCONTRAR(".",A1,1)-1),"000") & "." & TEXTO(EXTRAE(A1,ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)+1,ENCONTRAR(".",A1, ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)+1)-ENCONTRAR(".",A1,ENCONTRAR(".",A1,1)+1)-1), "000") & "." & TEXTO(DERECHA(A1,LARGO(A1)-ENCONTRAR(".",A1,ENCONTRAR(".",A1,ENCONTRAR( ".",A1,1)+1)+1)),"000")
This comment was minimized by the moderator on the site
Thanks for your help on translating the formula.
This comment was minimized by the moderator on the site
This is great! Very much appreciated. It would be nice to add to the VB code to reverse (put back) the IP's in the original format (without the extra leading 0's) after having sorted them. :) I would be very interested in that where you can run this script, sort results, then revert back to original format.
This comment was minimized by the moderator on the site
thank you, very useful !
This comment was minimized by the moderator on the site
la formule traduite en Francais :
=TEXTE(GAUCHE(I6;TROUVE(".";I6;1)-1);"000") & "." & TEXTE(STXT(I6;TROUVE( ".";I6;1)+1;TROUVE(".";I6;TROUVE(".";I6;1)+1)-TROUVE(".";I6;1)-1);"000") & "." & TEXTE(STXT(I6;TROUVE(".";I6;TROUVE(".";I6;1)+1)+1;TROUVE(".";I6; TROUVE(".";I6;TROUVE(".";I6;1)+1)+1)-TROUVE(".";I6;TROUVE(".";I6;1)+1)-1); "000") & "." & TEXTE(DROITE(I6;NBCAR(I6)-TROUVE(".";I6;TROUVE(".";I6;TROUVE( ".";I6;1)+1)+1));"000")
This comment was minimized by the moderator on the site
Merci pour la traduction !
This comment was minimized by the moderator on the site
Why does the formula not work for the last octet? It adds zeros to the 3rd octet but not the last? So frustrating.
This comment was minimized by the moderator on the site
I have tested the formula before I post it, it can work for the last octer. Have you checked the formula you pasted is correct?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations