如何在Excel中快速將IP地址從低到高排序?
通常,我們在Excel中使用Sort函數對字符串進行排序。 但是,如果需要一些IP地址進行排序,則直接使用“排序”功能可能會導致排序順序錯誤,如下面的屏幕截圖所示。 現在,我有一些方法可以在Excel中快速正確地對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")
按 Enter 鍵,然後將填充手柄拖動到單元格上以應用此公式。
2.複製公式結果並將其作為值粘貼在下一列中。 看截圖:
![]() |
![]() |
3.保持粘貼值處於選中狀態,然後單擊 數據 > 將A到Z排序.
4。 在裡面 排序警告 對話,保持 擴大選擇 檢查。
5。 單擊 分類。 現在,IP地址已從低到高排序。
您可以刪除幫助器列。
按VBA排序IP地址
這是一個VBA代碼,也可以為您提供幫助。1。 按 Alt + F11鍵 啟用 Microsoft Visual Basic for Applications 窗口。
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

3. Then click Tools > Reference, and check Microsoft VBScript Regular Expressions 5.5 in the popping dialog.


4. Click OK and press F5 key, a dialog pops out to remind you to select a range to work.

5. Click OK. Then the IP addresses have been filled with zero.
6. Select the IP addresses and click Data > Sort A to Z to sort them.
Sort IP address by Text to Columns
Actually, the Text to Columns feature can do you a favor in Excel too.
1. Select the cells you use, and click Data > Text to Columns. See screenshot:

2. In the Convert Text to Columns Wizard dialog, do as below:
Check Delimited, and click Next;
Check Other and type . into the textbox, and click Next;
Select a cell next to the IP address to place the result. Click Finish.



3. The select all cells containing the IP addresses and the split cells, and click Data > Sort.

4. In the Sort dialog, clicking Add level to sort data from column B to E (the split cells). See screenshot:

5. Click OK. Now the columns have been sorted.

The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Sort comments by
#24856
This comment was minimized by the moderator on the site
0
0
#24857
This comment was minimized by the moderator on the site
Report
0
0
#27298
This comment was minimized by the moderator on the site
0
0
#27299
This comment was minimized by the moderator on the site
0
0
#33479
This comment was minimized by the moderator on the site
0
0
#35886
This comment was minimized by the moderator on the site
0
0
#37261
This comment was minimized by the moderator on the site
0
0
#37262
This comment was minimized by the moderator on the site
Report
0
0
There are no comments posted here yet