Excel Add Text Based on Cell Value

To display text based on another cell, you have to use the conditional function IF.

The function displays the value in the first argument (“Bottom 50%”) if the condition is met, otherwise it displays the text “Top 50%”.

It becomes more complicated when there more conditions. Let’s say that we need more conditions:

  • Bottom 25%
  • Bottom 50%
  • Top 50%
  • Top 25%

Now our function will look like this.

As you can see, it becomes really complicated even for such a low number of conditions.

Instead of using this way to create a formula for more conditions, you can create a function in VBA that will display the desired text.

Add text based on a cell value in VBA

To do this we are going to use a function with the Select … Case statement. The function will take one parameter, which is a reference to a cell.

Create a new module and enter the following formula.

Type =CheckValue(A2) into cell B2 and autofill for the rest of the cells in the column.

This is a great way to write such functions because it’s more readable and can be easily modified.

Tomasz Decker is an Excel specialist, skilled in data analysis and financial modeling.

Posted in vba