excel VBA, How to create a IP address format standard on textbox userform

Djamel_

New Member
Joined
Dec 3, 2022
Messages
3
Office Version
  1. 2019
Dear Expert,

I need to create on the excel VBA an userform that have a textbox that I can enter IP addresse standard format.

Thanks in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The following code will validate the IP address once the user clicks on the CommandButton. If the IP address is not valid, the code sets focus to the TextBox and highlights the text so that the user can correct it. You'll need to change the name of the TextBox and CommandButton accordingly. Here's the code, which needs to be placed in the UserForm code module...

VBA Code:
Private Sub CommandButton1_Click()

    If Not is_valid_ip(Me.TextBox1.Value) Then
        With Me.TextBox1
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Value)
        End With
    End If
    
    'etc
    '
    '
    
End Sub

Private Function is_valid_ip(ByVal ip As String) As Boolean

' IP address must be made up of 4 segments and in the format x.x.x.x
' the first segment (ie. X.x.x.x) cannot be 000
' each segment must be within [0,255]

    Dim octets As Variant
    Dim i As Long
    
    octets = Split(ip, ".")
    
    If UBound(octets) <> 3 Then
        is_valid_ip = False
        Exit Function
    End If
    
    If octets(0) = 0 Then
        is_valid_ip = False
        Exit Function
    End If
    
    For i = LBound(octets) To UBound(octets)
        If octets(i) < 0 Or octets(i) > 255 Then
            is_valid_ip = False
            Exit Function
        End If
    Next i
    
    is_valid_ip = True

End Function

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top