How to Use VBA IF Logic to Set a UserForm Score and Color in Excel
Question details
The user wants to automatically assign a numerical score and update the background color of a UserForm control based on the specific value range entered into a contract text box.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Creating an interactive UserForm where input values are evaluated dynamically to provide visual feedback (color changes) and assign priority levels or scores.
- Observed behavior
- The goal is to evaluate the input value instantly after the user updates the text field and successfully apply the correct score and color to another control using VBA.
Ensure the Developer tab is enabled in your Excel ribbon and that your workbook is saved as a Macro-Enabled Workbook (.xlsm) so your VBA code is not lost upon closing.
Implement If...ElseIf Logic in the AfterUpdate Event
Attach a conditional logic script to the specific text box control so that it triggers visual and value changes automatically upon user input.
By placing the VBA code within the TxtContract_AfterUpdate event, the script will execute automatically as soon as the user finishes typing and moves the cursor out of the text box.
The If...ElseIf statement is ideal for evaluating multiple thresholds (e.g., values under 150,000, under 500,000, and above) and applying corresponding properties like .BackColor.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer pane on the left, double-click your UserForm to open the visual designer. Right-click the 'TxtContract' text box and select 'View Code'.
In the code window, ensure 'TxtContract' is selected in the top-left dropdown. In the top-right dropdown, select 'AfterUpdate' to generate the event subroutine.
Inside the Private Sub TxtContract_AfterUpdate() block, paste the following logic: If TxtContract < 150000 Then ResValue = 1 ResValue.BackColor = vbGreen ElseIf TxtContract < 500000 Then ResValue = 3 ResValue.BackColor = RGB(255, 192, 0) Else ResValue = 5 ResValue.BackColor = vbRed End If
Press F5 or click the Run button to launch the UserForm. Type a value into the TxtContract text box and press Tab. The ResValue control should instantly update its score and background color.

Run Your VBA Code Seamlessly with WPS Spreadsheet
WPS Spreadsheet provides a comprehensive Macro and VBA development environment, allowing you to create customized UserForms, automate complex tasks, and use advanced conditional logic just like you would in other premium spreadsheet applications.
- 1. Download and Install WPS Office: Get the latest version of WPS Office and install it on your computer.
- 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the UserForm.
- 3. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click 'Visual Basic' to open the editor.
- 4. Run Your UserForm: Manage your existing code and test your UserForm logic directly within the WPS environment.

Frequently Asked Questions
Why isn't the AfterUpdate event triggering in my UserForm?
The AfterUpdate event triggers only when the control loses focus. Ensure you are pressing Enter, pressing Tab, or clicking into another control after typing your value in the text box.
Can I use a Select Case statement instead of If...ElseIf?
Yes, a Select Case statement is a great alternative for evaluating ranges and can often be cleaner to read. You can use 'Case Is < 150000' within a 'Select Case TxtContract.Value' block to replicate this exact logic.
Why am I getting a Type Mismatch error when running this code?
A Type Mismatch error can occur if the 'TxtContract' text box is empty or contains non-numeric text when the AfterUpdate event fires. You should wrap your logic in an 'If IsNumeric(TxtContract.Value) Then' check to prevent this error.




