How to Validate an IP Address in an Excel VBA UserForm TextBox
Question details
The user needs to validate data input in a VBA UserForm TextBox so that it strictly accepts valid IPv4 addresses in standard dotted-decimal format.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Developing an Excel VBA UserForm that requires users to input IP addresses reliably without triggering errors for partial inputs during typing.
- Observed behavior
- Input needs to be verified to ensure it consists of exactly four numeric parts separated by periods, with each numeric value strictly falling between 0 and 255.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that the Developer tab is enabled on your ribbon so you can access the VBA editor.
Validate IP Address on Form Submit
Trigger the validation script when the user clicks a submit button, avoiding premature error messages while the IP address is being typed.
Validating on the TextBox 'Change' event is not recommended for complex strings like IP addresses. Because an IP address takes multiple keystrokes to enter (e.g., typing '192' will trigger errors at '1' and '19'), real-time validation will frustrate the user. Applying validation to the Submit button ensures the code only evaluates the complete entry.
The most effective logic involves splitting the string into an array using the period as a delimiter, checking that the array contains exactly four segments, confirming each segment is a number, and finally verifying that each number is between 0 and 255.
Press ALT + F11 to open the VBA editor. Ensure you have a UserForm inserted containing a TextBox (e.g., TextBox1) and a CommandButton (e.g., CommandButton1).
Double-click your CommandButton to open the Click event code block where the validation logic will reside.
Declare a string array and use the VBA Split function to divide the TextBox value by periods. For example: Dim IPParts() As String, IPParts = Split(TextBox1.Value, ".").
Use UBound(IPParts) to check the size of the array. An IPv4 address must yield an upper bound of 3 (meaning 4 segments indexed 0 to 3). If it does not, trigger an error message and exit the subroutine.
Use a For Loop to iterate through each item in IPParts. For each segment, use IsNumeric() to ensure it is a valid number, then check if CInt(segment) is >= 0 and <= 255. If any condition fails, display a MsgBox warning and stop execution.

Execute VBA and Macros Seamlessly in WPS Spreadsheet
WPS Spreadsheet offers robust support for macros and VBA, allowing you to design user forms and write custom data validation logic just as you would in standard spreadsheet applications.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your Macro-Enabled Spreadsheet document.
- 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon. If it is hidden, you can enable it in the WPS Options menu.
- 3. Open the VBA Editor: Click the 'Visual Basic' icon or press ALT + F11 to launch the editor and begin coding your IP validation logic.
- 4. Create UserForms: Use the Insert menu within the editor to add UserForms, TextBoxes, and CommandButtons directly into your project.

Frequently Asked Questions
Why shouldn't I validate the IP address on the TextBox Change event?
Validating on the Change event means the script checks the input after every single keystroke. Because formatting an IP address requires multiple characters and periods, users will constantly trigger false 'invalid' errors while they are still in the middle of typing.
How do I break down the IP address string in VBA?
You can use the built-in VBA `Split` function. By coding `Split(TextBox1.Value, ".")`, the application breaks the dotted-decimal string at every period and stores the resulting numeric chunks into an array for individual checking.
What is the best way to verify a segment is a number before checking its value?
Use the `IsNumeric()` function in your loop. It evaluates the string segment and returns True if it can be parsed as a number, preventing type mismatch errors when you later check if the value is between 0 and 255.
Can I run my Excel VBA UserForms containing validation logic in WPS Office?
Yes, WPS Office provides extensive compatibility for VBA macros and UserForms, enabling you to run, edit, and troubleshoot your existing Excel scripts directly in WPS Spreadsheet.




