VBA & Macro Problems

How to Use VBA IF Logic to Set a UserForm Score and Color in Excel

Adam DavisAdam Davis Nov 2, 2026 869 views

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.

How to Use VBA IF Logic to Set a UserForm Score and Color in Excel
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Access the UserForm Code

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'.

3
Select the AfterUpdate Event

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.

4
Insert the Conditional Logic

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

5
Test the UserForm

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.

Implement If...ElseIf Logic in the AfterUpdate Event
Color Constants and RGB: You can use built-in VBA color constants like vbGreen and vbRed, or use the RGB(red, green, blue) function to specify exact custom colors, such as RGB(255, 192, 0) for an orange/gold hue.
Advanced Macro Support in WPS Office

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. 1. Download and Install WPS Office: Get the latest version of WPS Office and install it on your computer.
  2. 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the UserForm.
  3. 3. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click 'Visual Basic' to open the editor.
  4. 4. Run Your UserForm: Manage your existing code and test your UserForm logic directly within the WPS environment.
Full support for Excel VBA macros, modules, and UserFormsHighly compatible with Microsoft Office .xlsm and .xlsb file formatsBuilt-in Visual Basic editor for writing, debugging, and testing codeLightweight application that loads heavy macro-enabled workbooks quickly
microsoft office alternative - wps office

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.