简体   繁体   中英

Conditional Formatting based on number in a cell that contains both text and numbers

如果单元格A1的值为“ 600 T”,是否有办法基于该数字应用小于/介于/大于条件格式的数字,而实际上忽略了该单元格中可能存在的任何文本?

=VALUE(LEFT(A1,3))<=X
=VALUE(LEFT(A1,3))>=X

Checks if first 3 characters (if a number) of A1 is less or greater than X.

=IF(AND(VALUE(LEFT(A1,3))>=X,VALUE(LEFT(A1,3))<=Y),TRUE,FALSE)

Checks if first 3 characters (if a number) of A1 is between X and Y.

MID or RIGHT can be used instead to check digits in the middle or end of a string.

Boolean operators such as AND already return TRUE or FALSE.

So this:

=IF(AND(VALUE(LEFT(A1,3))>=X,VALUE(LEFT(A1,3))<=Y),TRUE,FALSE)

… is equivalent to:

=AND(VALUE(LEFT(A1,3))>=X,VALUE(LEFT(A1,3))<=Y)


You can cast strings to numbers by multiplying by 1.

So this:

=LEFT(A1,3)*1

… is equivalent to:

 =LEFT(A1,3)*1 


Using these techniques reduces your formula:

 =IF(AND(VALUE(LEFT(A1,3))>=X,VALUE(LEFT(A1,3))<=Y),TRUE,FALSE) 

… to this:

=LEFT(A1,FIND(" ",A1))


Since your numbers may also have 4 digits, you can return the numeric portion like this:

 =LEFT(A1,FIND(" ",A1)) 

This does returns the space: "600 " … but that's not a problem since you're casting it to a number.

So your final formula would look like this:

 =AND(LEFT(A1,FIND(" ",A1))*1>=X,LEFT(A1,FIND(" ",A1))*1<=Y) 

Update

If your numbers don't include text, the above formula will fail with #VALUE!, because FIND is looking for a space that doesn't exist.

You can fix this by appending a space to FIND 's second argument. So your true final formula will look like this:

 =AND(LEFT(A1,FIND(" ",A1&" "))*1>=X,LEFT(A1,FIND(" ",A1&" "))*1<=Y) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM