Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to validate cells to only accept IP address in Excel?

While using Excel, do you know how to set up a column or a range of cells to only accept IP address format (xxx.xxx.xxx.xxx)? This article provides several methods for you to deal with it.

Validate cells to only accept IP address with Data Validation function

Validate cells to only accept IP address with VBA code

Enable you to do smarter, faster and better

Kutools - the best office productivity tool brings 300 advanced features to Excel. Details... Download...

Office Tab - brings tabbed interface to Excel and Word, make your work much easier. Details...Download...


Validate cells to only accept IP address with Data Validation function


The Data Validation function can help you to restrict valid entries on specified cells to only allow IP address is entered in Excel. Please do as follows.

1. Select the cells you will only allow IP address format entering, then click Data > Data Validation. See screenshot:

2. In the Data Validation dialog box, select Custom in the Allow drop-down list, enter formula =AND((LEN(A1)-LEN(SUBSTITUTE(A1,".","")))=3,ISNUMBER(SUBSTITUTE(A1,".","")+0)) into the Formula box, and then click the OK button.

Note: In the formula, A1 is the first cell of the selected cell range. Please change it as you need.

From now on, you can only enter IP address format content into these specified cells.

If unmatched format content has been detected, a warning box will pop up as below screenshot shown.


Validate cells to only accept IP address with VBA code

You can also apply the below VBA code to validate cells to only accept IP address format in Excel. Please do as follows.

1. Open the worksheet you will validate cells, right click the sheet tab and click View Code from the context menu.

2. In the opening Microsoft Visual Basic for Applications window, copy the below VBA code into it.

VBA code: validate cells to only accept IP address in Excel

Private Sub Worksheet_Change(ByVal Target As Range)
'Update by ExtendOffice 2018/10/25
Dim xArrIp() As String
Dim xIntIP1, xIntIP2, xIntIP3, xIntIP4 As Integer
If Intersect(Target, Range("A1:A20")) Is Nothing Then
    Exit Sub
Else
    If Target = "" Then
        Exit Sub
    End If
    xArrIp = Split(Target.Text, ".")
    If UBound(xArrIp) <> 3 Then
        GoTo EIP
    Else
    xIntIP1 = CInt(xArrIp(0))
    xIntIP2 = CInt(xArrIp(1))
    xIntIP3 = CInt(xArrIp(2))
    xIntIP4 = CInt(xArrIp(3))
    If (xIntIP1 < 1) Or (xIntIP1 > 255) _
    Or (xIntIP2 < 1) Or (xIntIP2 > 255) _
    Or (xIntIP3 < 1) Or (xIntIP3 > 255) _
    Or (xIntIP4 < 1) Or (xIntIP4 > 255) Then
    GoTo EIP
     End If
    End If
End If
Exit Sub
EIP:
    MsgBox "Please enter correct IP address"
    Target = ""
End Sub

Note: In the code, A1:A20 is the cell range you will validate the input type. Please change the range based on your needs.

3. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

From now on, you can only enter IP address format content into these specified cells.

If unmatched format content is entered, a warning box will pop up as below screenshot shown.



Excel Productivity Tools

Kutools for Excel - The Best Office Productivity Tool

  • Brings 300 powerful advanced features to Excel, enable you to do smarter, faster and better.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Office Tab - brings tabbed interface to Office, 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!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.