Excel Formulas to Clean and Remove Leading Zeros from IP Addresses

📅 Mar 04, 2026 📝 Sarah Miller

Managing network logs often presents a frustrating hurdle: IP addresses cluttered with non-standard leading zeros. While standard Excel tools like Text-to-Columns or find-and-replace functions offer a logical starting point for data cleanup, they frequently corrupt the underlying IP structure. Fortunately, a robust, dynamic formula grants database analysts a seamless, automated way to standardize datasets instantly. Under the stipulation that we are processing standard IPv4 formats, specific nested functions must be used to isolate and cleanse each octet. For instance, this methodology reliably converts 192.168.001.009 into 192.168.1.9. Below, we examine the precise formula structure to streamline your data.

Excel Formulas to Clean and Remove Leading Zeros from IP Addresses

When working with network logs, system exports, or firewall databases, you will often run into IPv4 addresses formatted with leading zeros (e.g., 192.168.001.005 or 010.000.000.020). While this padding ensures consistent character lengths across logs, it causes significant headaches in Excel. Standard IP utility tools, database queries, and lookup functions expect standard IPs without these leading zeros (e.g., 192.168.1.5 and 10.0.0.20).

Attempting a simple "Find and Replace" for 0 will break your IP addresses entirely, turning 100.100.100.100 into 1.1.1.1. To clean these strings safely and accurately, we must target individual octets. This comprehensive guide covers several ways to accomplish this, ranging from modern Microsoft 365 formulas to classic Excel workarounds, Power Query, and VBA.

Method 1: The Modern Excel Formula (Microsoft 365 & Excel 2021)

If you are using Microsoft 365 or Excel 2021, you have access to dynamic arrays and powerful new string-manipulation functions. You can resolve this issue with a remarkably short and elegant formula that avoids complex nesting.

The Formula

=TEXTJOIN(".", TRUE, VALUE(TEXTSPLIT(A2, ".")))

How It Works

This formula leverages dynamic array engine processing step-by-step:

  • TEXTSPLIT(A2, "."): This function splits the IP address string in cell A2 using the period (.) as a delimiter. It outputs a horizontal array of four text strings, such as {"192", "168", "001", "005"}.
  • VALUE(...): This wraps the array. By passing the split text strings into VALUE, Excel coerces them into numbers. During this process, Excel naturally drops any leading zeros: {"192", "168", "001", "005"} becomes {192, 168, 1, 5}.
  • TEXTJOIN(".", TRUE, ...): Finally, TEXTJOIN stitches the numerical array back together into a single string, inserting a period (.) between each number. The TRUE argument instructs the function to skip any empty elements if they occur.

Method 2: The Classic Formula (Excel 2019, 2016, & Older)

If you are working in an older desktop version of Excel, or sharing worksheets with users on legacy versions, you cannot use TEXTSPLIT. Instead, you must use a traditional approach that parses each of the four segments individually using text-extraction formulas.

The Formula

To safely isolate and convert each octet without dynamic arrays, we use the classic "space padding" trick:

=VALUE(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), 1, 100))) & "." &
VALUE(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), 101, 100))) & "." &
VALUE(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), 201, 100))) & "." &
VALUE(TRIM(MID(SUBSTITUTE(A2, ".", REPT(" ", 100)), 301, 100)))

How It Works

This formula may look daunting, but it uses a highly reliable, standardized logic:

  1. SUBSTITUTE(A2, ".", REPT(" ", 100)): This replaces every single period in the IP address with 100 spaces. It transforms a standard string like 010.001.050.002 into a massive string with wide blocks of spaces between the numbers.
  2. MID(..., Start_Num, 100): Because each segment is now safely isolated within massive blocks of spaces, we can grab 100-character segments from fixed points:
    • Character 1 to 100 contains the 1st octet.
    • Character 101 to 200 contains the 2nd octet.
    • Character 201 to 300 contains the 3rd octet.
    • Character 301 to 400 contains the 4th octet.
  3. TRIM(...): This removes all the trailing and leading spaces from each extracted segment, leaving us with isolated text values like "010", "001", "050", and "002".
  4. VALUE(...): Converting these trimmed strings into actual numerical values forces Excel to eliminate any leading zeros (yielding 10, 1, 50, and 2).
  5. & "." &: Finally, we concatenate the four cleaned numbers back together using ampersands and literal periods.

Method 3: Power Query (Best for Large Datasets)

If you are working with thousands of IP addresses, writing formulas can slow down your workbook calculations. Power Query is built for data transformation and handles cleaning processes like this with ease.

Step-by-Step UI Process

  1. Select your table containing the IP addresses.
  2. Navigate to the Data tab on the Ribbon and click From Table/Range. This opens the Power Query Editor.
  3. Right-click the header of the column containing your messy IP addresses and select Split Column > By Delimiter.
  4. Choose Custom, type a period (.) as the delimiter, and ensure Each occurrence of the delimiter is selected. Click OK. Excel splits your single IP column into four separate columns.
  5. Select all four newly created columns, right-click, and select Change Type > Whole Number. This instantly drops all leading zeros.
  6. Keep the four columns selected (in order from left to right), right-click the headers, and select Merge Columns.
  7. In the Merge dialog, choose Custom as the separator, type a period (.), name your new column "Cleaned IP", and click OK.
  8. Go to the Home tab and click Close & Load to return your cleaned data to a new Excel worksheet.

Power Query M Code

If you prefer to paste the raw M code directly into the Advanced Editor, you can use the following pattern:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column" = Table.SplitColumn(Source, "IP Address", Splitter.SplitTextByDelimiter(".", QuoteStyle.None), {"IP.1", "IP.2", "IP.3", "IP.4"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column",{{"IP.1", Int64.Type}, {"IP.2", Int64.Type}, {"IP.3", Int64.Type}, {"IP.4", Int64.Type}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"IP.1", type text}, {"IP.2", type text}, {"IP.3", type text}, {"IP.4", type text}}, "en-US"),{"IP.1", "IP.2", "IP.3", "IP.4"},Combiner.CombineTextByDelimiter(".", QuoteStyle.None),"Cleaned IP")
in
    #"Merged Columns"

Method 4: VBA User-Defined Function (UDF)

For workbooks where you need to clean IP addresses regularly but want to keep formulas simple, you can write a short VBA macro to create a custom formula: =CleanIP(A2).

The VBA Code

Function CleanIP(ByVal IPAddress As String) As String
    Dim Octets() As String
    Dim i As Long
    
    ' Split the string by dots
    Octets = Split(IPAddress, ".")
    
    ' Verify the IP has 4 parts
    If UBound(Octets) <> 3 Then
        CleanIP = "Invalid IP Format"
        Exit Function
    End If
    
    ' Loop through each octet and convert to integer to drop leading zeros
    For i = 0 To 3
        If IsNumeric(Octets(i)) Then
            Octets(i) = CStr(Val(Octets(i)))
        Else
            CleanIP = "Invalid IP Character"
            Exit Function
        End If
    Next i
    
    ' Join them back together
    CleanIP = Join(Octets, ".")
End Function

How to Install the VBA Code

  1. Press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
  2. Click Insert > Module in the top menu.
  3. Paste the VBA code above into the blank module window.
  4. Close the VBA Editor. You can now use =CleanIP(A2) directly inside your workbook. (Remember to save your workbook as an Excel Macro-Enabled Workbook with an .xlsm extension).

Summary of Solutions

Method Excel Version Compatibility Pros Cons
TEXTJOIN & TEXTSPLIT Office 365 / Excel 2021+ Short, simple, dynamic. Not backwards compatible.
SUBSTITUTE & REPT All Excel versions Highly compatible, no macros required. Long, complex to read and write.
Power Query Excel 2010 (with add-in) to current Excellent for large datasets, repeatable. Does not recalculate in real-time.
VBA UDF Excel Desktop (Windows & Mac) Very clean cell formulas. Requires macro-enabled workbook safety clearance.

Using any of these options will successfully strip out unnecessary leading zeros from your datasets, giving you standard, usable IP addresses. For modern workbooks, the Office 365 formula is the easiest to maintain, while Power Query is the best system for repetitive data loading pipelines.

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.