Ajusting number based on test

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
I have 2 columns. in column 1 I have information with how long I purchased/renewed a domain. I need column b to show just the # of years that are in column a. So b1 would have 1, b2 would have 2, b3 would have 1, b4 would have 2. Anyone have an idea of how to do this with a macro or a formula? Preferrably a formula if possible.

example:

Code:
             A                     B  
.com renewal 1 year       |                      | 
.com renewal 2 years      |                      | 
.net registration         |                      | 
.net registration 2 years |                      |

The acctual data looks like this ".COM Domain Name Renewal - 1 Year" but some only look like this... "Other Type Of Services". I tried to make this formula =RIGHT(A2,LEN(A2)-FIND("- ",A2)-1) which is leaving "1 Year" and I just need it to say "1" I also need it to just show a 1 instead of #VALUE! on other types.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
What I have so far...

This is what I have so far...

Code:
=IF(ISERROR(RIGHT(a2,LEN(a2)-FIND("- ",a2)-1)),1,(RIGHT(a2,LEN(a2)-FIND("- ",a2)-1)))

It is leaving either the #1 if there is an error which is perfect, but when it seperates the years it says "1 Year" or "2 Years", where I just need a 1 or a 2 or 5 or whatever. Anyone have any ideas?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,721
If each text string contains both a dash and the word 'year', try...

=MID(A2,FIND("-",A2)+1,(FIND("year",A2)-1)-FIND("-",A2))+0

To return 0 when no number is present, try the following instead...

=LOOKUP(9.99999999999999E+307,CHOOSE({1,2},0,MID(A2,FIND("-",A2)+1,(FIND("year",A2)-1)-FIND("-",A2))+0))

Hope this helps!
 

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
Excelletn

I modified that slightly to make the following... I'm sure there is a more eficcient way of doing it, but it works for me!

Code:
=IF(ISERROR(MID(C2,FIND("-",C2)+1,(FIND("Year",C2)-1)-FIND("-",C2))+0),1,(MID(C2,FIND("-",C2)+1,(FIND("Year",C2)-1)-FIND("-",C2))+0))
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,721

ADVERTISEMENT

How about...

=IF(ISNUMBER(FIND("-",C2)),MID(C2,FIND("-",C2)+1,(FIND("year",C2)-1)-FIND("-",C2))+0,1)

Hope this helps!
 

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
OK so I thought everything was working, except that sometimes year is lower case, and sometimes the Y is uppercase... Is there a way to make it always work?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,721

ADVERTISEMENT

Use SEARCH instead. It's not case sensitive. So, for example, change...

(FIND("year",C2)-1)

to

(SEARCH("year",C2)-1)

Hope this helps!
 

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
Ok another problem. Some of the cells contain data that looks like this: ".COM Registration (500) - 2 years" and for some reason it doesn't work on those cells...
 

Rbartlem

Board Regular
Joined
Jun 9, 2004
Messages
172
I'm sorry that was messed up. It says ".com registration bulk (250 - 500) - 1 year" so the first hyphen is throwing it all off...
 

Forum statistics

Threads
1,141,158
Messages
5,704,636
Members
421,360
Latest member
Rhodia

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
Top