Combining 2 formulas into 1 cell

mibarron

New Member
Joined
Feb 14, 2018
Messages
7
Hello,
Long time viewer of this site, first time poster. I'm not sure if this is possible, but is there a way to combine these two formulas into one?

=IFERROR(LEFT(D2,FIND(" ",D2)-1),D2)
=IF(LEN(D2)>9,LEFT(D2,LEN(D2)-20),D2)

Both work great individually, but I really need to combine them. I just can't seem to make it work. Thank you in advance for your help!!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi

given the second formula, you would have an error if D2 was less that 20 characters long?

Could you possible explain what is that you need to achieve, with some sample data, as there may be a different approach that works??
 
Upvote 0
As Edmitchell said...


As is... you'd have to change you IFERROR for an IF(ISERROR


i.e.

=IF(ISERROR(LEFT(D2,FIND(" ",D2)-1)),D2,IF(LEN(D2)>9,LEFT(D2,LEN(D2)-20),D2))


However, as edmitchell said, this formula (unless you are certain about the length/structure of your data) is quite likely to cause errors.
 
Upvote 0
Thank you so much for responding. I think that could be my issue. The problem is I am trying to select a text string that has combinations of capital words and lowercase. What I need are the capital words only, but here is the complexity: in most cases, I only need the first capital word, in about 20% of the other cases, I need the first 2 capitalized words. However, there are instances where other words on the string are capitalized, but I don’t want them. This is where the -20 comes in. Both formulas work individually to solve my problem, but I need something that can do both. Does this make sense? I forgot today was Valentine’s Day, so I had to leave work now & am typing from my phone. I was able to leverage a VBA UDF from this site to pull out all capilized words in the string, but that’s about as far as my VBA skills go, I’m afraid.
 
Upvote 0
That's a wonderfully different problem!

So you have data like:


Excel 2010
A
Raw Data
OVERTEACHING WIZARDRY
Undogmatic DOWINESS Matriarchal merenguing extermination Reinterrupt
dowiness PYCNIDIUM Stung Sthenic
PACKTHREADED LATINITY

<colgroup><col style="width: 25pxpx"><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

</tbody>
Sheet5




1. Is the data in each cell separated by a comma, a space, or some other kind of delimiter?

e.g. Fred, Tom, BILL, Mary

or

Fred Tom BILL Mary



2. You say: in about 20% of the other cases, I need the first 2 capitalized words.

What is the criteria for this? How do you know to pick these ones?
 
Upvote 0
Try this:


Excel 2010
AB
OVERTEACHING WIZARDRYOVERTEACHING,WIZARDRY
Undogmatic DOWINESS Matriarchal merenguing extermination ReinterruptDOWINESS
dowiness PYCNIDIUM Stung SthenicPYCNIDIUM
PACKTHREADED LATINITYPACKTHREADED,LATINITY
subvirile LUNCHER THAMYRIS Recolor purgerLUNCHER,THAMYRIS
Semiacid merenguing Jahwistic REINTERRUPT SERMONIZEDREINTERRUPT,SERMONIZED
jahwistic OVERTEACHING Overteaching NONREGENERATIVE MowraOVERTEACHING,NONREGENERATIVE
alleluiatic SERMONIZED SUBEMARGINATESERMONIZED,SUBEMARGINATE
extermination Overrealistic undogmatic MYATONIAMYATONIA
Descension Loan Reobtain
luncher Dues Nonadventitious jahwistic overrealistic LUNCHERLUNCHER
recolor Visaed CORNHUSKING REPEOPLE DegradinglyCORNHUSKING,REPEOPLE

<tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #D9D9D9"]Raw Data[/TD]
[TD="bgcolor: #D9D9D9"]Output[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

[TD="align: center"]10[/TD]

[TD="align: center"]11[/TD]

[TD="align: center"]12[/TD]

[TD="align: center"]13[/TD]

</tbody>


[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B2[/TH]
[TD="align: left"]=getupper(A2," ",2,",")[/TD]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "][/TH]
[TD="align: left"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



with this macro code

Code:
Public Function GetUpper(ByVal TextIn As String, ByVal Delim As String, ByVal NumToReturn As Integer, ByVal OPDelim As String) As String


    Dim IsUpper()
    Dim TopLim As Integer
    Dim Uppers As Boolean
    Dim Cntr As Integer
    Dim Cntr2 As Integer
    Dim InChar As String * 1
    Dim UppersFound As Integer
    Dim OutputStr As String
    
    
    WordArray = Split(Trim(TextIn), Delim)
    OutputStr = ""
    
    TopLim = UBound(WordArray)
    ReDim IsUpper(TopLim)
    
    If NumToReturn < 1 Or NumToReturn > TopLim + 1 Then
        GetUpper = CVErr(xlErrNum)
        Exit Function
    End If
    
    For Cntr = 0 To TopLim
        For Cntr2 = 1 To Len(WordArray(Cntr))
            InChar = Mid(WordArray(Cntr), Cntr2, 1)
            Uppers = True
            If (InChar Like "[A-Z]") Then
              Else
                Uppers = False
                
                Exit For
            End If
        Next Cntr2
        IsUpper(Cntr) = Uppers
        If Uppers = True Then
            UppersFound = UppersFound + 1
            If UppersFound = 1 Then
                OutputStr = WordArray(Cntr)
              Else
                If UppersFound <= NumToReturn Then OutputStr = OutputStr & OPDelim & WordArray(Cntr)
            End If
        End If
        
    Next Cntr


    GetUpper = OutputStr


End Function



The structure of the User Defined Formula: GetUpper() is as follows:



GetUpper( Var1, Var2, Var3, Var4)

Where

Var1 = the cell containing the string with various words, some upper case, some lower case - the source data

Var2 = the delimiter of the words. Are they separated by spaces? e.g. "The quick brown fox."

Then use " "


if delimited by commas e.g. "The,Quick,Brown,Fox"

then use ","


Var3 = The number of upper case words you want returned

If you put 1 here, you'll get the first one.

If you put 2, you'll get the first two, and so on.

This value must be between 1 and the max number of words in the source string


Var4 = How you want the outputted uppercase words separated (if more than one is returned)

If you want them separated by just commas then put ","
If just spaces: " "
if comma and space ", "
and so on.


example:

=GetUpper(A1, " ",2,",")



Hope this helps... and makes sense?
 
Upvote 0
I apologize for the delay in responding.

MartyS, Thank you so much for your help!! It worked, but I couldn't get it to do what I needed. Here's a brief example of what is happening using the formulas I mentioned earlier. I don't how to insert a picture properly. But the first column is an example of the naming convention. The second column in the result. The third column is the formula used to get me what I need. The fourth column are the results that are correct or incorrect. I hope this helps in clarifying what is happening. The file I am using is a data dump from a financial system, with over a million rows. Thanks again for your help!

Name Results Formula used C / (I)[TABLE="width: 883"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD] MS AD [/TD]
[TD]MS[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] MS AD [/TD]
[TD]MS AD[/TD]
[TD]IF(LEN(D1064)>8,LEFT(D1064,LEN(D1064)-20),D1064)[/TD]
[TD]Correct[/TD]
[/TR]
[TR]
[TD] VOLKSWC VOLKSWAGEN AG [/TD]
[TD]VOLKSWC[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Correct[/TD]
[/TR]
[TR]
[TD] VOLKSWC VOLKSWAGEN AG [/TD]
[TD]V[/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] SABICC SABIC [/TD]
[TD="align: center"]#VALUE![/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] HARISRC RPO [/TD]
[TD="align: center"]#VALUE![/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] HARISRC RPO [/TD]
[TD]HARISRC[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Correct[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Sorry, I meant to say on average their's roughly about 40-50,00 rows. Not a million. Can't seem to multi-task today. :)
 
Upvote 0
I apologize for the delay in responding.

MartyS, Thank you so much for your help!! It worked, but I couldn't get it to do what I needed. Here's a brief example of what is happening using the formulas I mentioned earlier. I don't how to insert a picture properly. But the first column is an example of the naming convention. The second column in the result. The third column is the formula used to get me what I need. The fourth column are the results that are correct or incorrect. I hope this helps in clarifying what is happening. The file I am using is a data dump from a financial system, with over a million rows. Thanks again for your help!

Name Results Formula used C / (I)[TABLE="width: 883"]
<tbody>[TR]
[TD] MS AD[/TD]
[TD]MS[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] MS AD[/TD]
[TD]MS AD[/TD]
[TD]IF(LEN(D1064)>8,LEFT(D1064,LEN(D1064)-20),D1064)[/TD]
[TD]Correct[/TD]
[/TR]
[TR]
[TD] VOLKSWC VOLKSWAGEN AG[/TD]
[TD]VOLKSWC[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Correct[/TD]
[/TR]
[TR]
[TD] VOLKSWC VOLKSWAGEN AG[/TD]
[TD]V[/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] SABICC SABIC[/TD]
[TD="align: center"]#VALUE![/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] HARISRC RPO[/TD]
[TD="align: center"]#VALUE![/TD]
[TD]IF(LEN(D31352)>9,LEFT(D31352,LEN(D31352)-20),D31352)[/TD]
[TD]Incorrect[/TD]
[/TR]
[TR]
[TD] HARISRC RPO[/TD]
[TD]HARISRC[/TD]
[TD]IFERROR(LEFT(D31353,FIND(" ",D31353)-1),D31353)[/TD]
[TD]Correct[/TD]
[/TR]
</tbody>[/TABLE]




Could you please explain the logic behind the output that you want, from the given input.


What exactly are you looking for? How specifically do you arrive at whether something is correct or not?

Rather than using different formulae, perhaps explain how you manually work out the correct output/result you're after.




With the data



[MS AD]

Why is [MS] incorrect, yet [MS AD] correct?




With the data


VOLKSWC VOLKSWAGEN AG

Why is [VOLKSWC VOLKSWAGEN] incorrect, yet [VOLKSWC] correct?




What are the RULES for evaluating what you want?



It seems that you have logic that says:

If there are two uppercase "words" whose total length is less than 8 characters, return BOTH words.
If there are two uppercase "words" BUT the total length is more than 8 characters, return ONLY the first uppercase word



If you could define what you want, it's relatively easy to produce.

With the macro I created you could get the above results with =GetUpper(A1, " ", IF(LEN(getupper(A1, " ", 2, " ") )> 8, 1,2)," ")


..but until I know how you work it out I can't be certain.


If you could perhaps provide:

Column 1 - source data
Column 2 - correct result
Column 3 - explanation why it's correct

e.g.


[TABLE="width: 1037"]
<tbody>[TR]
[TD]Source Data[/TD]
[TD]Correct Result[/TD]
[TD]Reasoning/Logic[/TD]
[/TR]
[TR]
[TD]MS AD[/TD]
[TD]MS AD[/TD]
[TD]First uppercase word is only two characters. First two words come to 5 characters. This is less than 8, so select both[/TD]
[/TR]
[TR]
[TD]VOLKSWC VOLKSWAGEN AG[/TD]
[TD]VOLKSWC[/TD]
[TD]First uppercase word is 7 characters. Next word over 8 character limit. Use only first word[/TD]
[/TR]
[TR]
[TD]SABICC SABIC[/TD]
[TD]SABICC[/TD]
[TD]First uppercase word is 6 characters. Next word over 8 character limit. Use only first word[/TD]
[/TR]
[TR]
[TD]HARISRC RPO[/TD]
[TD]HARISRC[/TD]
[TD]First uppercase word is 7 characters. Next word over 8 character limit. Use only first word[/TD]
[/TR]
[TR]
[TD]A B C D E F G H[/TD]
[TD]A B C D[/TD]
[TD]First 4 uppercase words together come to less than 8 characters.[/TD]
[/TR]
[TR]
[TD]POT AMBRIDGE[/TD]
[TD]POT[/TD]
[TD]First uppercase word is 3 characters. Next word over 8 character limit. Use only first word[/TD]
[/TR]
[TR]
[TD]ABC DEF GHI[/TD]
[TD]ABC DEF[/TD]
[TD]First 2 uppercase words together less than 8 characters. Next word over 8 character limit. Use only first 2 words.[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Could you please explain the logic behind the output that you want, from the given input.


It seems that you have logic that says:

If there are two uppercase "words" whose total length is less than 8 characters, return BOTH words.
If there are two uppercase "words" BUT the total length is more than 8 characters, return ONLY the first uppercase word

If you could define what you want, it's relatively easy to produce.

With the macro I created you could get the above results with =GetUpper(A1, " ", IF(LEN(getupper(A1, " ", 2, " ") )> 8, 1,2)," ")
This is actually what I need, I think. I'll try it and see if it works for me. Thanks!!!

Your assumption is correct. These are ID codes in a long text string. I was able to narrow the focus to pull just capitalized words. The ID Codes are typically no more than 8 characters, but the text strings is up to 70 characters long in total and the naming conventions used are not the same as many hands enter in the data using different variations. This is why MS AD is correct, because it is withing the 8 characters, without pulling in the extra words.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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