Complicated IF formula (how do I combine 11 if statements)

alinka

Board Regular
Joined
Oct 30, 2002
Messages
70
Hi Excel wizards,

I am a newbie to excel and have a complicated problem. See spreadsheet below for reference, plz.

This is what I need to do:

1) I regularly get a large amount of name data in columns A through C. Each row has an email syntax in Column D. In Column E, I created a formula that writes an email address for the corresponding syntax. I have 11 email syntaxes in total.

2) I need a formula that can read an email syntax in D, and use one of my 11 formulas to create an email address. In other words, I want the formula to do this:

a) IF we find "first.last" in D, use formula =A2&"."&C2&(MID(D2,(FIND("@",D2)),50))

b) IF we find "first_last" in D, use formula =A3&"_"&C3&(MID(D3,(FIND("@",D3)),50))

c) IF we find "first.m.last" in D, use formula =A4&"."&(IF(B4="","",(LEFT(B4,1))&"."))&C4&(MID(D4,(FIND("@",D4)),50))

and so on for the next 8 syntaxes that are shown in D.

So, there are 11 IF statements, that need to be combined into 1 formula. How can I go about doing this??? Or do you envision an easier way? Is it possible to write a vb code that would do this? ANY HELP IS APPRECIATED. THANKS!!!!
email-syntax.xls
ABCDE
1FirstNameMiddleLastNameEmailSyntaxEmailFormula
2AlanJLacyfirst.last@company.comAlan.Lacy@company.com
3WilliamCWhitefirst_last@company.comWilliam_White@company.com
4MichaelBuxtonfirst.m.last@company.comMichael.Buxton@company.com
5JeromeBeKarlinfirst_m_last@company.comJerome_B_Karlin@company.com
6WilliamJackRudolphsenfirst_m_last@company.comWilliam_J_Rudolphsen@company.com
7MarkAWagnerflast@company.comMWagner@company.com
8RobertMKrallastf@company.comKralR@company.com
9WilliamLHubbsfmlast@company.comWLHubbs@company.com
10AlanJLacyfirstlast@company.comAlanLacy@company.com
11JeromeKarlinskyFLast>7@company.comJKarlins@company.com
12WilliamRudolphsenFLast>8@company.comWRudolphs@company.com
Sheet1
 
Re: Complicated IF formula (how do I combine 11 if statement

Hello Zack

Sorry I called you firefytr. I just noticed that you are Zack
You are good in Excel

:-P

My first name is Pekka
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
firefytr said:
Hello,

You can also assign the macro to drawings or Auto Shapes for some creative latitude. Hope this helps.


Cool ;o)
Thanks, this I did not know. Now I can make some picturesque amazing links to my macros.

Best regards
Pekka

:o :o :o
 
Upvote 0
Well thanks for the kind words Pekka! I appreciate that, although I'm still learning much.

Another method, slightly more dynamic, would be to set your data up like this:
Book1
ABCDEF
1FirstNameMiddleLastNameISPSyntaxEmailAddy
2AlanJLacycompany.comfirst.last@company.comAlan.Lacy@company.com
3WilliamCWhitevbaexpress.comfirst_last@company.comWilliam_White@vbaexpress.com
4MichaelBuxtonhotmail.comfirst.m.last@company.comMichael__Buxton@hotmail.com
5JeromeBeKarlinyahoo.comfirst_m_last@company.comJerome_B_Karlin@yahoo.com
6WilliamJackRudolphsentheofficeexperts.comfirst_m_last@company.comWilliam_J_Rudolphsen@theofficeexperts.com
7MarkAWagnercomcast.comflast@company.comMWagner@comcast.com
8RobertMKraljuno.comlastf@company.comKralR@juno.com
9WilliamLHubbsvbaexpress.comfmlast@company.comWLHubbs@vbaexpress.com
10AlanJLacymsn.comfirstlast@company.comAlanLacy@msn.com
11JeromeKarlinskyaol.comFLast>7@company.comJKarlins@aol.com
12WilliamRudolphsenverizon.comFLast>8@company.comWRudolphs@verizon.com
Sheet1


The blue colored cells in column E are the syntax's that you have specified. Probably best put in Data Validation -> List -> In-cell drop down box. The orange column F are the cells that the macro was run in. The macro is only slightly different in the formula area:<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> emailAddy2()

    <SPAN style="color:#00007F">Dim</SPAN> a<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, b<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, c<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, d<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, e<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> f<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, g<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, h<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, j<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, k<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> i<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>
    a = "first.last@company.com":   b = "first_last@company.com"
    c = "first.m.last@company.com": d = "first_m_last@company.com"
    e = "flast@company.com":        f = "lastf@company.com"
    g = "fmlast@company.com":       h = "firstlast@company.com"
    j = "FLast>7@company.com":      k = "FLast>8@company.com"
    i = ActiveCell.Row
    <SPAN style="color:#00007F">Dim</SPAN> x<SPAN style="color:#00007F">As</SPAN> String: x = Range("E" & i).Value
    <SPAN style="color:#00007F">Select</SPAN><SPAN style="color:#00007F">Case</SPAN> x
        <SPAN style="color:#00007F">Case</SPAN> a
            Range("F" & i).Formula = "=A" & i & "&"".""&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> b
            Range("F" & i).Formula = "=A" & i & "&""_""&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> c
            Range("F" & i).Formula = "=A" & i & "&""_""&LEFT(B" & i & ",1)&""_""&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> d
            Range("F" & i).Formula = "=A" & i & "&""_""&(IF(B" & i & "="""","""",(LEFT(B" & i & ",1))&""_""))&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> e
            Range("F" & i).Formula = "=LEFT(A" & i & ",1)&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> f
            Range("F" & i).Formula = "=C" & i & "&LEFT(A" & i & ",1)&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> g
            Range("F" & i).Formula = "=LEFT(A" & i & ",1)&LEFT(B" & i & ",1)&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> h
            Range("F" & i).Formula = "=A" & i & "&C" & i & "&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> j
            Range("F" & i).Formula = "=(LEFT(A" & i & ",1))&(LEFT(C" & i & ",7))&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN> k
            Range("F" & i).Formula = "=LEFT(A" & i & ",1)&LEFT(C" & i & ",8)&""@""&D" & i
        <SPAN style="color:#00007F">Case</SPAN><SPAN style="color:#00007F">Else</SPAN>
            Range("F" & i).Formula = """"
    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Select</SPAN>
    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Re: Complicated IF formula (how do I combine 11 if statement

Zack,

I must be as thick as two telephone books as I could not get your macro to work. The following works for me with the OP’s posted data (columns A:D).
Code:
Option Explicit
ResultSub emailAddy3()
Dim i As Long
Dim y As Long
Dim last_row As Long
Dim c As Variant
Dim rng As Range

last_row = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row

Application.ScreenUpdating = False

For i = 2 To last_row
Select Case Cells(i, "D").Value
    Case "first.last@company.com"
        Range("E" & i).Formula = _
          "=A" & i & "&"".""&C" & i & "&(MID(D" & i & ",(FIND(""@"",D" & i & ")),50))"
    Case "first_last@company.com"
        Range("E" & i).Formula = _
          "=A" & i & "&""_""&C" & i & "&(MID(D" & i & ",(FIND(""@"",D" & i & ")),50))"
    Case "first.m.last@company.com"
        Range("E" & i).Formula = _
          "=A" & i & "&""_""&LEFT(B" & i & ",1)&""_""&C" & i & "&MID(D" & i & ",FIND(""@"",D" & i & "),50)"
    Case "first_m_last@company.com"
        Range("E" & i).Formula = _
          "=A" & i & "&""_""&(IF(B" & i & "="""","""",(LEFT(B" & i & ",1))&""_""))&C" & i & "&(MID(D" & i & ",(FIND(""@"",D" & i & ")),50))"
    Case "flast@company.com"
        Range("E" & i).Formula = _
          "=LEFT(A" & i & ",1)&C" & i & "&MID(D" & i & ",(FIND(""@"",D" & i & ")),50)"
    Case "lastf@company.com"
        Range("E" & i).Formula = _
          "=C" & i & "&LEFT(A" & i & ",1)&MID(D" & i & ",(FIND(""@"",D" & i & ")),50)"
    Case "fmlast@company.com"
        Range("E" & i).Formula = _
          "=LEFT(A" & i & ",1)&LEFT(B" & i & ",1)&C" & i & "9&MID(D" & i & ",(FIND(""@"",D" & i & ")),50)"
    Case "firstlast@company.com"
        Range("E" & i).Formula = "=A" & i & "&C" & i & "&(MID(D" & i & ",(FIND(""@"",D" & i & ")),50))"
    Case "FLast>7@company.com"
        Range("E" & i).Formula = _
          "=(LEFT(A" & i & ",1))&(LEFT(C" & i & ",7))&(MID(D" & i & ",(FIND(""@"",D" & i & ")),50))"
    Case "FLast>8@company.com"
        Range("E" & i).Formula = _
          "=LEFT(A" & i & ",1)&LEFT(C" & i & ",8)&MID(D" & i & ",(FIND(""@"",D" & i & ")),50)"
    Case "firstlast@vianet.net.au"
         Range("E" & i).Formula = _
    "=LEFT(A" & i & ",3)&MID(D" & i & ",(FIND(""@"",D" & i & ")),50)"
    Case Else
        Range("E" & i).Formula = """"
End Select
Next

With Columns("E:E")
  .HorizontalAlignment = xlLeft
  .Value = .Value      ' remove the formula
End With

' add the email hyperlink
For Each c In Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row)
If c = "" Then Exit For
ActiveSheet.Hyperlinks.Add Anchor:=c, Address:= _
   "mailto:" & c, TextToDisplay:=c.Text
Next c

Application.ScreenUpdating = True
End Sub
Results:
Email - formatting.xls
ABCDE
1FirstNameMiddleLastNameEmailSyntaxEmailAddress
2AlanJLacyfirst.last@company.comAlan.Lacy@company.com
3WilliamCWhitefirst_last@company.comWilliam_White@company.com
4MichaelBuxtonfirst.m.last@company.comMichael__Buxton@company.com
5JeromeBeKarlinfirst_m_last@company.comJerome_B_Karlin@company.com
6WilliamJackRudolphsenfirst_m_last@company.comWilliam_J_Rudolphsen@company.com
7MarkAWagnerflast@company.comMWagner@company.com
8RobertMKrallastf@company.comKralR@company.com
9WilliamLHubbsfmlast@company.comWL@company.com
10AlanJLacyfirstlast@company.comAlanLacy@company.com
11JeromeKarlinskyFLast>7@company.comJKarlins@company.com
12WilliamRudolphsenFLast>8@company.comWRudolphs@company.com
Sheet1


After creating the formulas in column E, the macro turns the formulas into a value and then creates an active email hyperlink.

Regards,

Mike
 
Upvote 0
That is strange Mike. Works for me, but I'm trying it also on a cell by cell basis. I also like the way you did your case select, as it should be. :) Little easier that way. The only thing I can think of is if you tried it with your data setup the way you posted it. Mine took into account for custom ISP's in another column. I re-posted the code a second time with this in mind. Whichever one you try, use the layout of html in that post when testing. Let me know if you still can't get it (if you want to).

Another thing I did notice is that the syntax and formulas didn't always match up from the OP's spec's. This is easy enough to change, but wasn't sure if they wanted it or not.
 
Upvote 0
Re: Complicated IF formula (how do I combine 11 if statement

Hi guys,

I did some tests -- of both Firefytr and Ekim's vb codes -- unfortunately, they don't work properly.

Firefytr - your code gives a result of a " (quote) instead of an email address and it does it row by row, what am I doing wrong. I need something that will work on all rows, if possible.

Ekim, I posted your code into a module -- and get this error: Compile Error: Invalid Outside Procedure.

I have revised my data to make it easier for me and for you guys to write the code. I took Firefytr's advice and decided to separate the SYNTAX into two parts: email Prefix, for ex: first.last and email suffix, for ex. Company.net...This way, I wrote a new formula for each of 11 syntaxes -- and now, maybe you guys can help me with the vb code. I even managed to write an IF statement that works!!! (I can't believe that I managed to do that on my own) -- but, this if statement only fits 7 of my syntaxes, and I need it to be more expandable, as I have 11 syntaxes and might be adding more syntaxes in the future.

hERE'S THE SPREADSHEET SO FAR -- I can even send it to you guys if needed. thanks so much in advance!!
Alina-SyntaxFormulaV1.xls
ABCDEFGH
1FirstNameMiddleLastNameEmailSyntaxEmailPrefixEmailSuffixADDRESSnotes
2AlanJLacyfirst.last@company.comfirst.lastcompany.comAlan.Lacy@company.com
3WilliamCWhitefirst_last@company.comfirst_lastcompany.comWilliam_White@company.com
4MichaelMoeBuxtonfirst.m.last@company.comfirst.m.lastcompany.comMichael.M.Buxton@company.comcheckw/outMiddleNameWORKS
5WilliamJackRudolphsenfirst_m_last@company.comfirst_m_lastcompany.netWilliam_J_Rudolphsen@company.net
6MarkAWagnerflast@company.comflastcompany.comMWagner@company.com
7RobertMKrallastf@company.comlastfcompany.eduKralR@company.edu
8WilliamLHubbsfmlast@company.orgfmlastcompany.orgWLHubbs@company.org
9JeromeKarlinskyflast7@company.comflast7company.comFALSEcantfitthelastsyntaxintoformula,max7ifstmts
Sheet1
 
Upvote 0
Ok Alinka, maybe you could try this:<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> emailAddy4()

    <SPAN style="color:#00007F">Dim</SPAN> i<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> Lrow<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>
    Lrow = Range("D65536").End(xlUp).Row

    <SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> rng<SPAN style="color:#00007F">In</SPAN> Range("G2:G" & Lrow)
        i = rng.Row
        <SPAN style="color:#00007F">Select</SPAN><SPAN style="color:#00007F">Case</SPAN> Range("D" & i).Value
            <SPAN style="color:#00007F">Case</SPAN> "first.last@company.com"
                Range("G" & i).Formula = "=A" & i & "&"".""&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "first_last@company.com"
                Range("G" & i).Formula = "=A" & i & "&""_""&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "first.m.last@company.com"
                Range("G" & i).Formula = "=A" & i & "&"".""&LEFT(B" & i & ",1)&"".""&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "first_m_last@company.com"
                Range("G" & i).Formula = "=A" & i & "&""_""&LEFT(B" & i & ",1)&""_""&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "flast@company.com"
                Range("G" & i).Formula = "=LEFT(A" & i & ",1)&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "lastf@company.com"
                Range("G" & i).Formula = "=LEFT(C" & i & ",1)&A" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "fmlast@company.com"
                Range("G" & i).Formula = "=LEFT(A" & i & ",1)&LEFT(B" & i & ",1)&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "firstlast@company.com"
                Range("G" & i).Formula = "=A" & i & "&C" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "lastfirst@company.com"
                Range("G" & i).Formula = "=C" & i & "&A" & i & "&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "FLast>7@company.com"
                Range("G" & i).Formula = "=LEFT(A" & i & ",1)&LEFT(C" & i & ",7)&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN> "FLast>8@company.com"
                Range("G" & i).Formula = "=LEFT(A" & i & ",1)&LEFT(C" & i & ",8)&""@""&F" & i
            <SPAN style="color:#00007F">Case</SPAN><SPAN style="color:#00007F">Else</SPAN>
                Range("G" & i).Formula = "No Set Syntax"
        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Select</SPAN>
        
        rng.Value = rng.Value
        <SPAN style="color:#00007F">If</SPAN> rng = ""<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">Exit</SPAN><SPAN style="color:#00007F">For</SPAN>
            ActiveSheet.Hyperlinks.Add Anchor:=rng, Address:= _
                "mailto:" & rng, TextToDisplay:=rng.Text
    <SPAN style="color:#00007F">Next</SPAN> rng<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN></FONT>

This will let you run anytime, not just one cell at a time (like my first one). Be careful not to have any data below (especially in column D) that could possibly interfere with the macro. Also, in actuality, column E isn't even used in this macro. But it could be good for any cross-checking/validating of sorts. Just as a double check, my data looked like this (orange was filled after the macro ran):
Book1
ABCDEFG
1FirstNameMiddleLastNameSyntaxFirstSuffixLastSuffixNoSetSyntax
2AlanJLacyfirst.last@company.comfirst.lastCompanyAlan.Lacy@Company
3WilliamCWhitefirst_last@company.comfirst_lastvbaexpress.comWilliam_White@vbaexpress.com
4MichaelH.Buxtonfirst.m.last@company.comfirst.m.lasthotmail.comMichael.H.Buxton@hotmail.com
5JeromeBeKarlinfirst_m_last@company.comfirst_m_lastyahoo.comJerome_B_Karlin@yahoo.com
6WilliamJackRudolphsenflast@company.comflasttheofficeexperts.comWRudolphsen@theofficeexperts.com
7MarkAWagnerlastf@company.comlastfcomcast.comWMark@comcast.com
8RobertMKralfmlast@company.comfmlastjuno.comRMKral@juno.com
9WilliamLHubbsfirstlast@company.comfirstlastvbaexpress.comWilliamHubbs@vbaexpress.com
10SmithChrislastfirst@company.comlastfirstsdpadres.comChrisSmith@sdpadres.com
11AlanJLacyFLast>7@company.comFLast>7msn.comALacy@msn.com
12JeromeKarlinskyFLast>8@company.comFLast>8aol.comJKarlinsk@aol.com
Sheet1


Note, this will also take out the formulas and replace them with email hyperlinks, a cross between Mike's and mine.

Hope this helps. :) edit: grammar
 
Upvote 0
Re: Complicated IF formula (how do I combine 11 if statement

Hi Zack (Fyreftr)

First, thank you so much for your help. I think I am much closer to solving my problem thanks to you and all the others on this board.

Ok, your code works on all rows and does almost what I need it to do...
The problem is that the code reads from column D - the "email syntax" column. I don't want it to read from column D, I want it to read from E & F. The reason is because D will never have "first.last@company.com"...it will always have the real company name there, i.e. "first.last@ibm.com"...basically I will not even have Column D, I will just have that column split into to: E & F...

Why I am saying this is because your code says this:
Select Case Range("D" & i).Value
Case "first.last@company.com"
Range("G" & i).Formula = "=A" & i & "&"".""&C" & i & "&""@""&F" & i

I want it to say something like this (forgive me if I don't write the code properly -- i have no clue in VB.

Select Case Range("E" & i).Value
Case "first.last"
Range("G" & i).Formula = "=A" & i & "&"".""&C" & i & "&""@""&F" & id

Let me know if I have confused you. You help is much appreciated it!!! THANKS.
 
Upvote 0
Re: Complicated IF formula (how do I combine 11 if statement

Also, another question. In the VB code, what does "i" stand for? (I'm trying to learn from your code so that I can expand it in the future. Thanks so much!

Also, I've seen this done before: a button that when pressed runs the macro. How do I do that?

Muchos Gracias!
 
Upvote 0
Re: Complicated IF formula (how do I combine 11 if statement

You can make the button as I explained earlier with form-tool-box or as Zack explained

"You can also assign the macro to drawings or Auto Shapes for some creative latitude. Hope this helps."

You assign the macro by clicking the Auto Shape ( a circle or so ) with your mouse right button.

:o :o
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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