Cell begins with excel

In this example, the goal is to test values in column B to see if they begin with a specific text string, which is «xyz» in the worksheet shown. This problem can be solved with the LEFT function, as explained below.

LEFT function

The LEFT function extracts a given number of characters from the left side of a text string. For example, the formula below returns the first three letters of «apple», which is «app»:

=LEFT("apple",3) // returns "app"

This means we can use the LEFT function to test if cell B5 begins with «xyz» like this:

=LEFT(B5,3)="xyz"

The LEFT function extracts the first 3 characters in cell B5 and the result is compared to the string «xyz» forcing a TRUE or FALSE result. The formula is solved like this:

=LEFT(B5,3)="xyz"
=LEFT("ABC-1224-HNN",3)="xyz"
="ABC"="xyz"
=FALSE

For cell B5 the result is FALSE, since «ABC-1224-HNN» does not begin with «xyz». In cell B6, however, the result is TRUE, since «XYZ-6543-JWB» does begin with «xyz».

=LEFT(B6,3)="xyz"
=LEFT("XYZ-6543-JWB",3)="xyz"
="XYZ"="xyz"
=TRUE

Note that Excel is not case-sensitive by default, so «XYZ»=»xyz» will return TRUE in a formula. Also note the num_chars argument is set to 3 above, but must be modified according to the situation. For example, to test for a value that begins with «apple», num_chars should be set to 5:

=LEFT(B5,5)="apple"

Case-sensitive option

Excel is not case-sensitive by default, but you can easily adapt the formula to use the EXACT function to make the formula case-sensitive like this:

=EXACT(LEFT(B5,3),"xyz")

EXACT takes two arguments, text1 and text2. EXACT will only return TRUE when text1 and text2 are exactly the same, taking into account case. For example:

=EXACT("abc","ABC") // returns FALSE
=EXACT("abc","Abc") // returns FALSE
=EXACT("abc","abc") // returns TRUE

Turning back to cell B6 in the worksheet shown, the two formulas below return different results:

=EXACT(LEFT(B6,3),"xyz") // returns FALSE
=EXACT(LEFT(B6,3),"XYZ") // returns TRUE

The first formula returns FALSE because the EXACT function is case-sensitive, so «XYZ» does not equal «xyz». The second formula returns TRUE because «XYZ» does equal «XYZ» taking into account case. Note we don’t need the equal to operator (=) in this formula since EXACT performs a comparison automatically.

If cell begins with

To adapt the formulas above to «If cell begins with», simply drop the formulas into the IF function as the logical test. For example, to return «Yes» when a cell contains «xyz» and «No» when not, you can use a formula like this

=IF(LEFT(B5,3)="xyz", "Yes", "No")

The case-sensitive version of the formula works the same way:

=IF(EXACT(LEFT(B5,3),"XYZ"), "Yes", "No")

Other functions

It is worth noting that Excel contains two other functions, the SEARCH function and the FIND function that are meant to look for a substring in a text value. They could be used to solve this problem, but they are more work to configure in this case, and the resulting formulas are more complicated, so I don’t see any advantage to using them.

Alternately, you could use the COUNTIF function with a wildcard to solve this problem like this:

=COUNTIF(B5,"xyz*")<>0

This works fine, but keep in mind that COUNTIF is in a group of eight *IFS functions that won’t accept an array for the range argument. This means you can’t use COUNTIF to test values in an array returned by another operation. I don’t like this limitation, so I avoid the *IFS functions when there is a good alternative.

In excel, it is possible to highlight cells that begin with a certain text using conditional formatting and a formula that returns TRUE when cell starts with the text as specified. To learn how to highlight cells that begin with a particular text in excel, read this illustrative post to the end.

Figure 1: How to highlight cells that begin with a given text string

General syntax of the formula

=SEARCH (“substring”, A1)=1

Understanding the formula

When a formula is used to apply a given conditional format, that formula is first evaluated based on the active cell in your selection. This is the case when you are creating the rule. In our case above, the rule we create shall be evaluated for each of the cells in the range A2:B7. A2 will then change to reflect the address of the cell being evaluated each time because it is entered as a relative address.

  • In this example, we want to highlight cells that begin with “Je”. To do this, we simply need to highlight the cells and apply conditional formatting.
  • In the formula bar, we enter the formula to be used as a rule, which is =SEARCH(“Je”,A2)=1
  • We then press Enter to highlight those cells that begin with “Je”

Example

Figure 2: Example of how to highlight cells that begin with “Je”

Note that you can also use excel built-in presets to highlight those cells that begin with a particular text string. To do this, you simply need to select the range where your data is, then click on “Conditional formatting”. After that, you will have to specify the text string that will define your rule.

This formula utilizes the SEARCH function in order to match cells that begin with your specific text string. If the text string is found, the formula will return a number. If not found, it will return a #VALUE! Error.

Instant Connection to an Expert through our Excelchat Service

Most of the time, the problem you will need to solve will be more complex than a simple application of a formula or function. If you want to save hours of research and frustration, try our live Excelchat service! Our Excel Experts are available 24/7 to answer any Excel question you may have. We guarantee a connection within 30 seconds and a customized solution within 20 minutes.

Are you still looking for help with Conditional Formatting? View our comprehensive round-up of Conditional Formatting tutorials here.

If you want to test values to see if they begin with some given specific characters like “x”, ”y”, or “z”, you can create a formula with COUNTIF and SUM functions to return results.

Table of Contents

  • EXAMPLE
  • FORMULA
  • FORMULA EXPLANATION
  • COMMENT
    • Related Functions

EXAMPLE

If Cell Begins with One of Three Supplied Characters

You can see “TRUE” or “FALSE” is returned properly based on the first letter in each cell.

In this example, we supply three letters “x”, “y” and “z” as the conditions of “begins with”, if texts begin with one of them, we will get “TRUE” after running the formula. If texts begin with other letters, “FALSE” is displayed after running the formula.

FORMULA

Refer to above example, the generic formula is:

=SUM(COUNTIF(A1, “X*”, “Y*”, “Z*”))>0

FORMULA EXPLANATION

COUNTIF is used for counting the numbers of values from a range or an array that meet a specified condition.  

Syntax: COUNTIF(Range, Criteria).

In Excel formula, we use “X*” to represent a value begins with “X”, the asterisk (*) is a wildcard to stand for one or more characters. So, values like “X”, “Xe” or “Xee” all meet the condition “begins with “X*””.

As we supply three letters “x”, “y”, “z” in this example, so we create an array constant {“x*”,”y*”,”z*”} to show the collection of criteria “begins with”. As the three letters are given as a precondition, so they are hard-coded, we use comma to separate them. An array constant is started with curly brace and ended with curly brace.

COUNTIF function compares text in cell with the items in array constant one by one. In this COUNTIF, as there is only one cell in range, and text can only begins with one of the three supplied letters or none of them, thus COUNTIF(A2,{“x*”,”y*”,”z*”}) can only returns “1” or “0” for each criteria after comparing with text in A2.

For example, in cell A2, text is “zje”, in array constant the first item is “x*”, obviously “zje” doesn’t meet the criteria, “0” is returned and saved in array, COUNTIF retrieves the next criteria “y*” to compare with “zje”, still returns “0”, and for the last criteria “z*”, “1” is returned.

In this example, we can get different results after running COUNTIF:

COUNTIF(A2,{“x*”,”y*”,”z*”})->COUNTIF(“zje”,{“x*”,”y*”,”z*”})->{0,0,1}

COUNTIF(A3,{“x*”,”y*”,”z*”}))>COUNTIF(“abc”,{“x*”,”y*”,”z*”})->{0,0,0}

After explanation, we can see there are only four results returned by COUNTIF: {0,0,1}, {1,0,0}, {0,1,0} and {0,0,0}. They are returned by COUNTIF and delivered to SUM function directly. SUM function then add up all items in the list, there are only two results “1” and “0”. We add “>0” after SUM() to compare SUM result with 0 and force result to “TRUE” of “FALSE”; if result is greater than 0, “TRUE” is the final result of SUM(COUNTIF())>0; if SUM result is 0, logical test fails, so “FALSE” is the final result.

  1. You can add more criteria per your demands if you have multiple criteria.
  2. It doesn’t require an exact match and is case-insensitive when comparing with text and criteria.
  3. To get “TRUE” or “FALSE” as final result, you can add logical test “>0” to force SUM evaluates to “TRUE” or “FALSE”.


  • Excel SUM function
    The Excel SUM function will adds all numbers in a range of cells and returns the sum of these values. You can add individual values, cell references or ranges in excel.The syntax of the SUM function is as below:= SUM(number1,[number2],…)…
  • Excel COUNTIF function
    The Excel COUNTIF function will count the number of cells in a range that meet a given criteria. This function can be used to count the different kinds of cells with number, date, text values, blank, non-blanks, or containing specific characters.etc.= COUNTIF (range, criteria)…

EXPLANATION
This tutorial shows how to count cells that begin with a specific value using Excel formulas and VBA.
This tutorial provides two Excel method that can be applied to count cells that begin with a specific value by using an Excel COUNTIF function and the «*» criteria. The first method references to a cell for the specific value and in the second method the specific value is directly entered into the formula. The formulas count the cells from range (B8:B14) that start with letter «b».
This tutorial provides two VBA methods that can be applied to count cells that begin with a specific value. The first method references to a cell for the specific value and in the second method the specific value is directly entered into the VBA code.

FORMULA (with value referenced to a cell)
=COUNTIF(range, value&»*»)

FORMULA (with value directly entered into the formula)
=COUNTIF(range, «value*»)

ARGUMENTS
range: The range of cells you want to count from.
value: The value that the cell begins with.

  1. 05-06-2015, 12:34 PM


    #1

    pauldaddyadams is offline


    Valued Forum Contributor


    If cell begins with

    Hi,

    What is the cleanest way of having an if formula where if cell A1 begins with either a 1,2,3,4,5 then return �Bals� if not return �OPER�?

    Paul


  2. 05-06-2015, 12:39 PM


    #2

    Re: If cell begins with

    Hi there. Try this, which will work with alphanumeric cells (you didn’t specify if that was the case) and will NOT return the wrong result if the first alphanumberic character is zero:
    =IF(AND(CODE(LEFT(A1,1))>48,CODE(LEFT(A1,1))<54),»Bals»,»Oper»)

    Last edited by Glenn Kennedy; 05-06-2015 at 12:45 PM.


  3. 05-06-2015, 12:40 PM


    #3

    Re: If cell begins with

    Hi

    And if the first character will always be a digit then

    Richard Buttrey

    RIP — d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.


  4. 05-06-2015, 12:43 PM


    #4

    Re: If cell begins with

    OR

    =IF(MIN(SEARCH({0,1,2,3,4,5},A1&1/17))=1,»Bals»,»OPER»)

    If you like my answer please click on * Add Reputation
    Don’t forget to mark threads as «Solved» if your problem has been resolved

    «Nothing is so firmly believed as what we least know.»
    Michel de Montaigne


  5. 05-06-2015, 12:44 PM


    #5

    Re: If cell begins with

    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.


  6. 05-07-2015, 03:11 AM


    #6

    pauldaddyadams is offline


    Valued Forum Contributor


    Re: If cell begins with

    Hi All

    wow — so many great responses! Thank you all


  7. 05-07-2015, 03:41 AM


    #7

    Re: If cell begins with

    Glad to have helped and thanks for the rep.


  8. 05-07-2015, 03:59 AM


    #8

    Re: If cell begins with


Понравилась статья? Поделить с друзьями:
  • Ceiling excel что это
  • Ceiling excel на русском
  • Cdate vba excel это
  • Ccleaner to proceed with this type the word erase here перевод ccleaner
  • Cbonds надстройка для excel