Custom cell format for IP address to force leading zeros?

4

I have a column of IP addresses I want to sort. I don't want to use hidden columns like this answer. What I would like to do is simply format the cells so that they force leading zeros. For instance, if I enter 192.168.1.1 it would show 192.168.001.001.

I'm not very good with custom formats, but I tried ###.###.###.### with no luck.

Is this possible?

Devil's Advocate

Posted 2018-06-19T14:23:01.527

Reputation: 1 397

1If you do not want to use helper columns then the only option is vba to do it in place. If you want a helper column, it can be done in one formula, but excel sees 192.168.1.1 as text and there for will not be able to put a mask on it. – Scott Craner – 2018-06-19T14:26:35.900

Custom formats wont affect the actual values of the Cells so they would still sort in the same way even if someone can find a format that you are seeking – PeterH – 2018-06-19T14:26:38.920

Answers

4

Select the cells that contain the addresses and run this short macro:

Sub IPFixer()
    Dim r As Range, arr

    For Each r In Selection
        arr = Split(r.Value, ".")
        For i = 0 To 3
            arr(i) = Format(arr(i), "000")
        Next i
        r.Value = Join(arr, ".")
    Next r
End Sub

Before:

enter image description here

and after:

enter image description here

EDIT#1:

Let's say we want this conversion to be automatically applied to entries in column A. Include the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range, rInt As Range
    Set rInt = Intersect(Range("A:A"), Target)
    If rInt Is Nothing Then Exit Sub
    Application.EnableEvents = False
        For Each r In rInt
                arr = Split(r.Value, ".")
                For i = 0 To 3
                    arr(i) = Format(arr(i), "000")
                Next i
                r.Value = Join(arr, ".")
        Next r
    Application.EnableEvents = True
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

Gary's Student

Posted 2018-06-19T14:23:01.527

Reputation: 15 540

If this included the code to automatically run this on any cell change in the specified column it would be an acceptable solution, in my opinion. But as it is now, anyone that enters a new IP to the sheet would have to know how to run this. – Devil's Advocate – 2018-06-20T14:05:16.980

1@ScottBeeson See my EDIT#1 – Gary's Student – 2018-06-20T14:17:03.147

Wow, you went above and beyond with the walkthrough and info on macros, thanks! – Devil's Advocate – 2018-06-20T14:36:57.350