How to check if a specific Column has Integer on each cell, and if it contains a string, Insert a blank cell to row in question.
asked Dec 10, 2009 at 13:50
Untested:
Dim row As Long
Dim col As Long
col = 4 ' Whatever column you want to check
For row = 1 To 100 ' How many rows you want to check
If Not IsNumeric(Cells(row, col).Value) Then
' Do whatever you want to do in this case
End If
Next row
If you clarify what you mean by «Insert a blank cell to row in question«, I will try to update my solution.
answered Dec 10, 2009 at 13:59
HeinziHeinzi
166k57 gold badges361 silver badges516 bronze badges
3
You can check even check with a forumla that the column contains no none-numbers only
=COUNTBLANK(AAA:AAA)-COUNTBLANK(B:B)=COUNT(B:B)
where I assume that column AAA:AAA is empty.
answered Dec 10, 2009 at 14:07
akuhnakuhn
27.2k2 gold badges76 silver badges91 bronze badges
A mix of the other answers for bigger data.
It first checks if the colomn has none Numbers only and if not, checks where.
Dim row As Long
Dim LastRow As Long
LastRow = Range("B" & Rows.Count).End(xlUp).Row 'if you want the colomn B
If Excel.WorksheetFunction.CountBlank(Range("AAA:AAA")) - Excel.WorksheetFunction.CountBlank(Range("B:B")) = Excel.WorksheetFunction.Count(Range("B:B")) Then
For row = 1 To LastRow
If Not IsNumeric(Range("B" & row).Value) Then
' Do whatever you want to do in this case
End If
Next row
End if
answered Nov 3, 2017 at 10:03
Pierre44Pierre44
1,7012 gold badges10 silver badges32 bronze badges
Function IsInt(i As Variant) As Boolean
'checks if value i is an integer. returns True if it is and False if it isn't
IsInt = False
If IsNumeric(i) Then
If i = Int(i) Then
IsInt = True
End If
End If
End Function
Function IsString(s As Variant) As Boolean
'checks if variable s is a string, returs True if it is and False if not
IsString = True
If s = "" Then
IsString = False
Else
If IsNumeric(s) Then
IsString = False
End If
End If
End Function
Sub CheckInts(c As Integer)
'c = column number to check
'goes through all cells in column c and if integer ignores, if string sets to ""
Dim r, lastrow As Integer
lastrow = Sheet1.UsedRange.rows.Count 'find last row that contains data
For r = 1 To lastrow
If Not IsInt(Cells(r, c).Value) Then
If IsString(Cells(r, c).Value) Then
Cells(r, c).Value = ""
End If
End If
Next r
End Sub
then just call CheckInts passing in the column number you want to change
e.g. CheckInts(2) will change column 2
answered Jul 3, 2020 at 13:55
OliOli
391 gold badge1 silver badge6 bronze badges
Return value
A logical value (TRUE or FALSE)
Usage notes
The ISNUMBER function returns TRUE when a cell contains a number, and FALSE if not. You can use ISNUMBER to check that a cell contains a numeric value, or that the result of another function is a number.
The ISNUMBER function takes one argument, value, which can be a cell reference, a formula, or a hardcoded value. Typically, value is entered as a cell reference like A1. When value is a number, the ISNUMBER function will return TRUE. Otherwise, ISNUMBER will return FALSE.
Examples
The ISNUMBER function returns TRUE if value is numeric:
=ISNUMBER("apple") // returns FALSE
=ISNUMBER(100) // returns TRUE
If cell A1 contains the number 100, ISNUMBER returns TRUE:
=ISNUMBER(A1) // returns TRUE
If a cell contains a formula, ISNUMBER checks the result of the formula:
=ISNUMBER(2+2) // returns TRUE
=ISNUMBER(2^3) // returns TRUE
=ISNUMBER(10 &" apples") // returns FALSE
Note: the ampersand (&) is the concatenation operator in Excel. When values are concatenated, the result is text.
Count numeric values
To count cells in a range that contain numbers, you can use the SUMPRODUCT function like this:
=SUMPRODUCT(--ISNUMBER(range))
The double negative coerces the TRUE and FALSE results from ISNUMBER into 1s and 0s and SUMPRODUCT sums the result.
Notes
- ISNUMBER will return TRUE for Excel dates and times since they are numeric.
- ISNUMBER will return FALSE for empty cells and errors.
Return to Excel Formulas List
Download Example Workbook
Download the example workbook
This tutorial demonstrates how to check if a cell is a number or text in Excel and Google Sheets.
Check Cell for Number or Text
We can check if a cell is a number by using the ISNUMBER Function or check if a cell is text with the ISTEXT Function.
ISNUMBER Function
The ISNUMBER Function checks if a value is a number. It returns TRUE if the value is a number and FALSE if it’s not.
=ISNUMBER(B3)
Knowing if a cell value is a number helps avoid errors when working with formulas. Numbers are sometimes stored as text, i.e. B4 (5000) in the example above, and that can result in errors in formulas referencing that cell.
In this example, the date, phone number, and time values all returned TRUE. These are regarded as numeric values in Excel and Google Sheets.
ISTEXT Function
The ISTEXT Function checks if a cell is text. It returns TRUE if the value is a text string and FALSE if it’s not.
=ISTEXT(B3)
In this example, although the value in B5 (2350) is a number, it’s written within quotes and therefore stored as a text string. That’s why it returns TRUE.
Check if Cell is a Number or Text in Google Sheets
These formulas (ISNUMBER and ISTEXT) work exactly the same in Google Sheets as in Excel.
EXPLANATION
tutorial shows how to test if a range contains a cell with a number and return a specified value if the formula tests true or false, by using an Excel formula and VBA.
This tutorial provides one Excel method that can be applied to test if a range contains at least one cell that has only numbers by using a combination of Excel IF, SUMPRODUCT and ISNUMBER functions. In this example, if the SUMPRODUCT and ISNUMBER part of the formula returns a value of greater than 0, meaning the range contains cells that only captures number, the test is TRUE and the formula will return a «Contains Number» value. Alternatively, if the SUMPRODUCT and ISNUMBER part of the formula returns a value of 0, meaning the range does not have any cells that only capture numbers, the test is FALSE and the formula will return a «No Number» value.
This tutorial provides one VBA method that can be applied to test if a range contains at least one cell that has only numbers by looping through each cell in a selected range and using the IsNumeric function to identify if a cell contains only numbers. As soon as the it identifies the first cell that contains only numbers it will return a value of «Contains Number» and exit loop. If the range does not contain any cells that contain only numbers, after looping through each cell in a range, it will return a value «No Number».
FORMULA
=IF(SUMPRODUCT(—ISNUMBER(range))>0,value_if_true, value_if_false)
ARGUMENTS
range: The range of cells you want to test if they contain only numeric values.
value_if_true: Value to be returned if the range contains at least one cell that has only numbers.
value_if_false: Value to be returned if the range does not contain any cells that contain only numbers.
Excel has a number of formulas that help you use your data in useful ways. For example, you can get an output based on whether or not a cell meets certain specifications. Right now, we’ll focus on a function called “if cell contains, then”. Let’s look at an example.
Jump To Specific Section:
- Explanation: If Cell Contains
- If cell contains any value, then return a value
- If cell contains text/number, then return a value
- If cell contains specific text, then return a value
- If cell contains specific text, then return a value (case-sensitive)
- If cell does not contain specific text, then return a value
- If cell contains one of many text strings, then return a value
- If cell contains several of many text strings, then return a value
Excel Formula: If cell contains
Generic formula
=IF(ISNUMBER(SEARCH("abc",A1)),A1,"")
Summary
To test for cells that contain certain text, you can use a formula that uses the IF function together with the SEARCH and ISNUMBER functions. In the example shown, the formula in C5 is:
=IF(ISNUMBER(SEARCH("abc",B5)),B5,"")
If you want to check whether or not the A1 cell contains the text “Example”, you can run a formula that will output “Yes” or “No” in the B1 cell. There are a number of different ways you can put these formulas to use. At the time of writing, Excel is able to return the following variations:
- If cell contains any value
- If cell contains text
- If cell contains number
- If cell contains specific text
- If cell contains certain text string
- If cell contains one of many text strings
- If cell contains several strings
Using these scenarios, you’re able to check if a cell contains text, value, and more.
Explanation: If Cell Contains
One limitation of the IF function is that it does not support Excel wildcards like «?» and «*». This simply means you can’t use IF by itself to test for text that may appear anywhere in a cell.
One solution is a formula that uses the IF function together with the SEARCH and ISNUMBER functions. For example, if you have a list of email addresses, and want to extract those that contain «ABC», the formula to use is this:
=IF(ISNUMBER(SEARCH("abc",B5)),B5,""). Assuming cells run to B5
If «abc» is found anywhere in a cell B5, IF will return that value. If not, IF will return an empty string («»). This formula’s logical test is this bit:
ISNUMBER(SEARCH("abc",B5))
Read article: Excel efficiency: 11 Excel Formulas To Increase Your Productivity
Using “if cell contains” formulas in Excel
The guides below were written using the latest Microsoft Excel 2019 for Windows 10. Some steps may vary if you’re using a different version or platform. Contact our experts if you need any further assistance.
1. If cell contains any value, then return a value
This scenario allows you to return values based on whether or not a cell contains any value at all. For example, we’ll be checking whether or not the A1 cell is blank or not, and then return a value depending on the result.
- Select the output cell, and use the following formula: =IF(cell<>»», value_to_return, «»).
- For our example, the cell we want to check is A2, and the return value will be No. In this scenario, you’d change the formula to =IF(A2<>»», «No», «»).
- Since the A2 cell isn’t blank, the formula will return “No” in the output cell. If the cell you’re checking is blank, the output cell will also remain blank.
2. If cell contains text/number, then return a value
With the formula below, you can return a specific value if the target cell contains any text or number. The formula will ignore the opposite data types.
Check for text
- To check if a cell contains text, select the output cell, and use the following formula: =IF(ISTEXT(cell), value_to_return, «»).
- For our example, the cell we want to check is A2, and the return value will be Yes. In this scenario, you’d change the formula to =IF(ISTEXT(A2), «Yes», «»).
- Because the A2 cell does contain text and not a number or date, the formula will return “Yes” into the output cell.
Check for a number or date
- To check if a cell contains a number or date, select the output cell, and use the following formula: =IF(ISNUMBER(cell), value_to_return, «»).
- For our example, the cell we want to check is D2, and the return value will be Yes. In this scenario, you’d change the formula to =IF(ISNUMBER(D2), «Yes», «»).
- Because the D2 cell does contain a number and not text, the formula will return “Yes” into the output cell.
3. If cell contains specific text, then return a value
To find a cell that contains specific text, use the formula below.
- Select the output cell, and use the following formula: =IF(cell=»text», value_to_return, «»).
- For our example, the cell we want to check is A2, the text we’re looking for is “example”, and the return value will be Yes. In this scenario, you’d change the formula to =IF(A2=»example», «Yes», «»).
- Because the A2 cell does consist of the text “example”, the formula will return “Yes” into the output cell.
4. If cell contains specific text, then return a value (case-sensitive)
To find a cell that contains specific text, use the formula below. This version is case-sensitive, meaning that only cells with an exact match will return the specified value.
- Select the output cell, and use the following formula: =IF(EXACT(cell,»case_sensitive_text»), «value_to_return», «»).
- For our example, the cell we want to check is A2, the text we’re looking for is “EXAMPLE”, and the return value will be Yes. In this scenario, you’d change the formula to =IF(EXACT(A2,»EXAMPLE»), «Yes», «»).
- Because the A2 cell does consist of the text “EXAMPLE” with the matching case, the formula will return “Yes” into the output cell.
5. If cell does not contain specific text, then return a value
The opposite version of the previous section. If you want to find cells that don’t contain a specific text, use this formula.
- Select the output cell, and use the following formula: =IF(cell=»text», «», «value_to_return»).
- For our example, the cell we want to check is A2, the text we’re looking for is “example”, and the return value will be No. In this scenario, you’d change the formula to =IF(A2=»example», «», «No»).
- Because the A2 cell does consist of the text “example”, the formula will return a blank cell. On the other hand, other cells return “No” into the output cell.
6. If cell contains one of many text strings, then return a value
This formula should be used if you’re looking to identify cells that contain at least one of many words you’re searching for.
- Select the output cell, and use the following formula: =IF(OR(ISNUMBER(SEARCH(«string1», cell)), ISNUMBER(SEARCH(«string2», cell))), value_to_return, «»).
- For our example, the cell we want to check is A2. We’re looking for either “tshirt” or “hoodie”, and the return value will be Valid. In this scenario, you’d change the formula to =IF(OR(ISNUMBER(SEARCH(«tshirt»,A2)),ISNUMBER(SEARCH(«hoodie»,A2))),»Valid «,»»).
- Because the A2 cell does contain one of the text values we searched for, the formula will return “Valid” into the output cell.
To extend the formula to more search terms, simply modify it by adding more strings using ISNUMBER(SEARCH(«string», cell)).
7. If cell contains several of many text strings, then return a value
This formula should be used if you’re looking to identify cells that contain several of the many words you’re searching for. For example, if you’re searching for two terms, the cell needs to contain both of them in order to be validated.
- Select the output cell, and use the following formula: =IF(AND(ISNUMBER(SEARCH(«string1»,cell)), ISNUMBER(SEARCH(«string2″,cell))), value_to_return,»»).
- For our example, the cell we want to check is A2. We’re looking for “hoodie” and “black”, and the return value will be Valid. In this scenario, you’d change the formula to =IF(AND(ISNUMBER(SEARCH(«hoodie»,A2)),ISNUMBER(SEARCH(«black»,A2))),»Valid «,»»).
- Because the A2 cell does contain both of the text values we searched for, the formula will return “Valid” to the output cell.
Final thoughts
We hope this article was useful to you in learning how to use “if cell contains” formulas in Microsoft Excel. Now, you can check if any cells contain values, text, numbers, and more. This allows you to navigate, manipulate and analyze your data efficiently.
We’re glad you’re read the article up to here Thank you
You may also like
» How to use NPER Function in Excel
» How to Separate First and Last Name in Excel
» How to Calculate Break-Even Analysis in Excel