If X is greater than Y in a cell

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I've got three formulae that work in isolation, but not together.

If the text "4 for 10" is in cell A1, I'd like one formula that returns the text "Multi deal where X<Y" if 4 is greater than 10, which it is. But returns "Multi deal where X>Y"if the text in cell A2 was 10 for 4.

The formulae which work in isolation are as follows:

This formula in cell B1 returns the word "for" if that text is in A1:

=IF(ISNUMBER(SEARCH("for",A1)),"For","")

This formula returns the first number on the left of A1 (if it has the word "for")

=RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)

This formula returns the number on the right of the cell (if it has the word "for")

=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))

But when I combine them together, I get an error message.

Eg this should return "Multi deal where X <Y" but it doesn't (if this formula is in cell D1). Does anyone know the solution, please?

=IF(and((ISNUMBER(SEARCH("for",A1)),"For",RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)>TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))),"Multi Deal where X>Y",IF(and((ISNUMBER(SEARCH("for",A1)),"For",RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)<TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))),"Multi Deal where X<Y",""))))
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
These are text

=RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)
=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))

You'll get a string comparison which will not be the correct result unless you do this

=RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)+0
=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))+0
 
Upvote 0
Try replacing
=RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2) with =VALUE(RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2))
and
=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100)) with =VALUE(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100)))

 
Upvote 0
Hi guys

I tried both solutions above, but neither of them worked?

They both returned a "There is a problem with this formula...." error message. Any other ideas?

The first one was

=IF(and((ISNUMBER(SEARCH("for",A1)),"For",""),RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)+0>TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))+0,"Multi Deal where X>Y",""))

And the second one was

=IF(and((ISNUMBER(SEARCH("for",A1)),"For",""),value(RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2))>value(TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100)))),"Multi Deal where X>Y","")
 
Upvote 0
Youve included the result "For","" in the first part of the formula

Your first formula should be this

=IF(AND(ISNUMBER(SEARCH("for",A1)),RIGHT(TRIM(LEFT(A1,SEARCH("for",A1)-1)),2)+0>TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))+0),"Multi Deal where X > Y","")
 
Last edited:
Upvote 0
Hi Special K

Do you know why this formula isn't working, as expected?

I've got four columns of data across two rows - you can copy and paste the data below into cells A1 to D2.

The data in cells A1 and A2 are formulae and the formula in cell A1 is =IF(AND(B1<>"non",ISNUMBER(C1)>0,ISNUMBER(D1)>0),"Fixed Multi Deal","Something else")

You can then drag down that formula to cell A2.

The formula checks if the word "Multi" is in a cell in column b AND if the values in columns C and D are numbers greater than 0. But it doesn't work for row 2. Do you know why? TIA

[TABLE="width: 446"]
<colgroup><col><col span="3"></colgroup><tbody>[TR]
[TD]Fixed Multi Deal[/TD]
[TD]Multi[/TD]
[TD]2[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Fixed Multi Deal[/TD]
[TD]non[/TD]
[TD]non[/TD]
[TD]non

[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Looking at the output 2 and 4 are left-justified which suggests they are text and not numbers (though they could indeed be numbers). Your output is Fixed Multi Deal so I'm assuming that the
row Multi 2 4 is correct and you're referring to the non non non row also producing a Fixed Multi Deal output.

I notice if I put "non" in C1 and do this ISNUMBER(C1) I get TRUE.
If I do this (as per your formula) ISNUMBER(C1)>0 I get FALSE.

Change this

ISNUMBER(C1)>0

to this

ISNUMBER(C1)

and do the same for D1

In a case like this where you have AND(condition1, condition2, condition3 etc) the easiest way is to take each condition and place it in a blank cell so you can isolate which conditions are working and which aren't.
 
Last edited:
Upvote 0
Hi Special K-99

Thanks for the prompt response and spotting the error.

Although that solution works on the sample data, it doesnt work on the real data, which seems odd.

I've broken it down to one example (just 5 columns and two rows):

Cells A1:D1 have the titles shown in the data below:

A1 = Mechanic
B1 = Code
C1 = Code if B2 is blank
D1 = X
E1 = Y

A2 has the text 4 for 10
B2 has this formula:=IF(AND(C2="MS",ISNUMBER(D2),ISNUMBER(E2)),"Fixed Multi Deal","")
C2 has this formula: =IF(A2="","",IF(ISNUMBER(SEARCH("for",A2)),"MS","Don’t touch this!"))
D2 has this formula: =IF(A2="","",IF(C2="MS",RIGHT(TRIM(LEFT(A2,SEARCH("for",A2)-1)),2),"Don’t touch this!"))
E2 has this formula: =IF(A2="","",IF(C2="MS",TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)),"Don't touch this!"))

Question - the formula in cells C2, D2 and E2 are working as intended. But the formula in B2 is not. Do you know why?

B2 should return the text "Fixed Multi Deal" if C2, D2, and E2 have "MS", 4 (or any number) and 10 (or any number), which they do! Your thoughts would be greatly appreciated.

Thanks in advance.
 
Upvote 0
All i can suggest is break it down again.

In a blank cell put =C2=MS to test the first part of B2.
In a blank cell put =ISNUMBER(D2) to test second part.
In a blank cell put =ISNUMBER(E2) to test third part.

My only query is D2 will return, blank, a RIGHT(...) or a string "Dont touch this" none of which are true numbers.
Same for E2

Try adding 0 to the RIGHT(...)+0 as stated previously to coerce the result into a number as returning a string may cause the ISNUMBER()s to fail.

Failing that, upload the file to a website and post a link to it here as the only way to test this properly is to check the actual data which is not possible by posting an image (which moderators prefer).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top