Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#331467 - 26/03/2010 20:41 A simple (I'm pretty sure) Excel question
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
Is there a function in Excel that will allow me to look at just the last two letters of a text string, and perform an operation based on what those letters are?

In other words, if I am in cell E12, I want to do something like this:

if [last_two_letters_C12]=ar,[append "áste" to the contents of E12],
if [last_two_letters_C12]=er,[append "íste" to the contents of E12],
if [last_two_letters_C12]=ir, [append "íste" to the contents of E12]

What I don't know how to do is parse out the last two letters of C12 to operate my if... statement.

Is there a simple way to do this? (Anybody who has had a year of high school español will know what I am trying to do here.) smile

tanstaafl.
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#331468 - 26/03/2010 21:20 Re: A simple (I'm pretty sure) Excel question [Re: tanstaafl.]
Geoff
enthusiast

Registered: 21/08/1999
Posts: 381
Loc: Northern Ireland
RIGHT(C12,2) should give the last 2 characters of the contents of C12, shouldn't it?
_________________________
Geoff
---- -------
Mk1 Blue - was 4GB, now 16GB
Mk2 Red - was 12GB, now 60GB

Top
#331471 - 26/03/2010 22:09 Re: A simple (I'm pretty sure) Excel question [Re: Geoff]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
Originally Posted By: Geoff
RIGHT(C12,2) should give the last 2 characters of the contents of C12, shouldn't it?


I dunno... that's why I was asking! smile

I'll give it a try and see what happens.

Thanks.

tanstaafl.

edit: Yes, indeed it does. Now I'll see if I can figure out the rest of it...

db

edit2: Well, it didn't take me long to figure out I was over my head again. How can I put a string into E12 that is C12 minus the last two characters, plus "áste" ?

In other words, if E12 contained the string Hablar, how would I convince cell E12 to contain the string Habláste, but only if the last two letters of E12 = "ar" ?

db


Edited by tanstaafl. (26/03/2010 22:27)
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#331476 - 26/03/2010 22:41 Re: A simple (I'm pretty sure) Excel question [Re: tanstaafl.]
Geoff
enthusiast

Registered: 21/08/1999
Posts: 381
Loc: Northern Ireland
I wasn't entirely sure either, I checked it in Openoffice, not Excel whistle grin

Can a formula append text to the contents of its own cell? Would that not introduce a circular reference?

It seems to me that an additional column is needed, say in F12, with something like:
Code:
=if(right(c12,2)="ar",concatenate(e12,"áste"),if(right(c12,2)="er",concatenate(e12,"íste"),if(right(c12,2)="ir",concatenate(e12,"íste"),e12)))

Then you could hide column E.

My Excel is very rusty though, so there may be a much better solution.

Edit: a better solution like a VBA macro, but my knowledge of those is even more rusty!


Edited by Geoff (26/03/2010 22:44)
_________________________
Geoff
---- -------
Mk1 Blue - was 4GB, now 16GB
Mk2 Red - was 12GB, now 60GB

Top
#331479 - 27/03/2010 00:53 Re: A simple (I'm pretty sure) Excel question [Re: Geoff]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
Originally Posted By: Geoff
circular reference?


Doh! Of course, you are right.

After giving it some thought, I think I can rephrase the problem (taking a different approach) so that it is possible. Maybe.

We have a string (Hablar, for example) in C12. The formula in E12 will check C12 to see if the string ends in "ar", and if it does it writes that string in C12 minus the "ar" ending and appends the "áste" string to it. (If C12 ended in "er" or "ir" it would append the appropriate string instead of "áste".)

A simple formula like

=if(right(c12,2)="ar",C12,"")

is not circular, but when we start getting fancy by writing only part of C12 to the destination cell and then appending something to it, I'm not sure what will happen.

tanstaafl.
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#331481 - 27/03/2010 02:19 Re: A simple (I'm pretty sure) Excel question [Re: tanstaafl.]
gbeer
carpal tunnel

Registered: 17/12/2000
Posts: 2665
Loc: Manteca, California
Code:
Private Sub Worksheet_Calculate()
        For Each C In Worksheets("Sheet1").Range("test").Cells
            C.Value = Right(C.Value, 2)
            
        Next
        
End


My install of Excel is broken, the help for VBA isn't working. But the above is a start.

If you rt-click on the sheet's tab, choose view code, and put the above in the code for that sheet. It will run whenever the spreadsheet recalculates. All it does is trim the contents of the cells in the named range to have only the last 2 chars. (you need to have a range named test predefined)

To complete this the comparison and revision code needs to be added and a suspension and resumption of auto recalculation has to be added, as this code runs endlessly. esc will exit the loop.


Edited by gbeer (27/03/2010 02:26)
_________________________
Glenn

Top
#331485 - 27/03/2010 03:09 Re: A simple (I'm pretty sure) Excel question [Re: tanstaafl.]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
If your input is in A1 and the thing you want to modify is in B1, then this works (in OpenOffice.org, anyway):

Code:
=IF(EXACT(RIGHT(A1;2);"ar");CONCATENATE(B1;"aste");IF(OR(EXACT(RIGHT(A1;2);"er");EXACT(RIGHT(A1;2);"ir"));CONCATENATE(B1;"iste");B1))


You have to put it in a separate cell. There's no way to modify the existing cell, AFAIK.
_________________________
Bitt Faulk

Top
#331498 - 28/03/2010 01:17 Re: A simple (I'm pretty sure) Excel question [Re: wfaulk]
gbeer
carpal tunnel

Registered: 17/12/2000
Posts: 2665
Loc: Manteca, California
Code:
Private Sub Worksheet_Change(ByVal words As Range)

        For Each C In Worksheets("Sheet1").Range("words").Cells
            If Right(C.Value, 2) = "ar" Then
                C.Value = Left(C.Value, (Len(C.Value) - 2)) + "áste"
            ElseIf Right(C.Value, 2) = "er" Then
                C.Value = Left(C.Value, (Len(C.Value) - 2)) + "íste"
            ElseIf Right(C.Value, 2) = "ir" Then
                C.Value = Left(C.Value, (Len(C.Value) - 2)) + "íste"
            End If
        Next
        
End Sub


Instructions:
In the worksheet select the range with all the text to be updated.
Name that range "words".

Select the worksheet's tab and rmb>view code.
Paste the above code into the window. Note: the name "Sheet1" in the code should be changed to match the name of the specific worksheet.

After that any change to a cell in the range "test" will trigger a check of the all the cells in the range "words" and make the replacements specified.
_________________________
Glenn

Top
#331512 - 28/03/2010 18:34 Re: A simple (I'm pretty sure) Excel question [Re: wfaulk]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
Originally Posted By: wfaulk
You have to put it in a separate cell. There's no way to modify the existing cell, AFAIK.


This is the last time I will ever be so naive as to think my Excel question might be simple! smile

Anyway, I went to my Excel Guru Extraordinaire (does anyone besides me feel the absence of Paul Grzelak on this bbs?) and here is the answer:

=IF(RIGHT(B3,2)="ar",LEFT(B3,LEN(B3)-2)&"áis",IF(RIGHT(B3,2)="er",LEFT(B3,LEN(B3)-2)
&"éis",IF(RIGHT(B3,2)="ir",LEFT(B3,LEN(B3)-2)&"ís","Notstandard")))

the key elements I was lacking being the "(LEN(B3 -2)" conceept, and the "&" function for appending the áis etc. suffixes.

Now I just have to work out how I am going to put the formula in its 18 variations (for 1st, 2nd, 3rd person singular and plural in present, past, and future tenses) into the spreadsheet in such a fashion that I can copy it and have the "B3" update to B7, B11, B15 etc. and still allow me to sort the list. Never mind, don't try to visualize that, without seeing the actual spreadsheet you can't make any sense of that.

At least now the problems are in the realm that I can understand.

tanstaafl.
_________________________
"There Ain't No Such Thing As A Free Lunch"

Top
#331526 - 28/03/2010 21:58 Re: A simple (I'm pretty sure) Excel question [Re: tanstaafl.]
gbeer
carpal tunnel

Registered: 17/12/2000
Posts: 2665
Loc: Manteca, California
Bitt is right. Formulas (functions) are not permitted to write to other cells. Nor can macros.

Excel only allows such things to happen in specific ways.

See this page for the long description. The exception noted is the one used in the VBA code I supplied.
_________________________
Glenn

Top
#331527 - 28/03/2010 22:08 Re: A simple (I'm pretty sure) Excel question [Re: gbeer]
gbeer
carpal tunnel

Registered: 17/12/2000
Posts: 2665
Loc: Manteca, California
You are not, by any chance, using Excel 2008 for Mac?
_________________________
Glenn

Top
#331535 - 29/03/2010 00:30 Re: A simple (I'm pretty sure) Excel question [Re: gbeer]
tanstaafl.
carpal tunnel

Registered: 08/07/1999
Posts: 5543
Loc: Ajijic, Mexico
Originally Posted By: gbeer
You are not, by any chance, using Excel 2008 for Mac?


No. I detest Macintosh computers. (Sorry, Bruno...) They're different from what I'm used to, so obviously they can't be any good. smile

I'm using Excel 2002.

See the attached file for the end result of my quest. Enter a verb and see what happens.

tanstaafl.


Attachments
ConjugationTestFile.xls (413 downloads)

_________________________
"There Ain't No Such Thing As A Free Lunch"

Top