+ Reply to Thread
Results 1 to 8 of 8

Formula combining CONCATENATE and IF functions

  1. #1
    Registered User
    Join Date
    03-28-2008
    Posts
    4

    Formula combining CONCATENATE and IF functions

    Hi everyone.

    I am trying to kill two birds with one stone in a single formula.

    I have three cells that I wish to combine to create an e-mail address cell.

    A. E-mail domain
    B. First name (F)
    C. Last name (L)

    However, a fourth cell comes in to play before cells B and C can be concatenated and that cell (D) is the e-mail convention (i.e. F.L, F_L, 1FL etc...) Where '1FL' = 'first letter of First Name followed by Last Name', or 'F1L' = 'First Name followed by first letter of Last Name', or '1F_L' = 'first letter of First Name followed by Last Name, separated by an underscore'.

    I have worked out how to concatenate the fields to the point of 'First Name . Last Name @ E-mail domain' ('= B3 & "." & C3 & "@" & A3"). So, one function is sorted - adding the '@' to the e-mail domain.

    What I am trying to work out is how to incorporate an IF function that will look at cell D, interrogate the e-mail convention and apply rules before concatenating cells B and C (e.g. - if e-mail convention in cell D is '1F.L', take just the first letter of First Name in cell B, then pull all of Last Name (cell C) into the resulting cell, whilst determining and adding the correct separator ('.' ; '_' ; or no separator).

    There are about eighteen (18) possible combinations of e-mail convention;

    1F.L
    1F_L
    1F1L
    1FL
    1LF
    F
    F.1L
    F.L
    F_1L
    F_L
    F1L
    FL
    L
    L.1F
    L.F
    L_F
    L1F
    LF

    Which makes the length of the formula mind boggling (for me, anyway)!

    Can anyone shed some light on how this can be done, please.

    Thanks,

    Ed.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,919
    If you don't mind a User Defined Function, then the following will work:
    Please Login or Register  to view this content.
    NOTE: As written, it requires that the list of conventions/rules be on sheet2, col A1 to A18. Change to suit.

    So, in cell E1, for example, enter =MakeAddr(A1,B1,C1,D1)
    and copy down your list
    Last edited by protonLeah; 03-29-2008 at 02:20 AM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    03-28-2008
    Posts
    4

    Concatenate & If

    Hi Ben.

    Thanks for the reply and apologies for the delay in responding. Truth is, I have absolutely no idea what to do with the answer! I've tried figuring it out (hence, the delay in replying), but I just don't know what to do with the information. Is there a menu option in Excel that would take me some where that I could paste your code?

    Sorry if this is a real mundane question to you. I am only conversant with entering formula in the cell.

    Cheers!

    Ed.

  4. #4
    Registered User
    Join Date
    03-28-2008
    Posts
    4

    Formula combining CONCATENATE and IF functions

    Hi Ben.

    Since my last post/reply, I have had a good crack at getting your code to work, but I keep getting errors.

    What I have done is copy and paste your user defined function in to a new macro, changed the field names to match exactly those of the worksheet, added a second worksheet with cells A1 to A18 containing the 18 various conventions, and pasted the function =MakeAddr(A2,B2,C2, D2) in to the first cell/record/row (E2).

    When I hit return on function in E2, I get an Excel error message "That name is not valid".

    And when I try running the Macro, I get a Visual Basic error message stating "Compile error: Ambiguous name deteceted: MakeAddr".

    Here's the macro;


    What have I done wrong?! I am pleased with how far I have progressed this (as an utter novice), and I appreciate your assistance in getting me this far, but I now really disappointed in myself that I cannot get it work.

    Can you please help?

    Cheers!

    Ed.
    Last edited by VBA Noob; 04-06-2008 at 04:26 PM.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,919
    First, to get the function to work, it must be inserted into a new module:
    In the project pane of the VB Editor, right-click the project (the one with the name of your file in parentheses), select: Insert -> module.
    The paste the function into the new module. Do not paste the lines
    Sub makeaddr(... or End Sub
    Note: a FUNCTION is a block of code that returns a single value. For example, say you enter the sentence "this is a test" in A1, then in B1 you enter the function "=LEN(A1)". When you press Enter, B1 will show the value 14 since LEN() counts the length of the string referenced (A1, here) and returns a single number.
    For this User Definded Function (UDF) it builds a string based on the parameters email_domain, first_name, etc and returns a single string: the e-mail address. SUBS do not return values, so you cannot have a sub named MakeAddr(... and enter "=MakeAddr(...) and have it return a value. So the SUB lines must be deleted. The changes you made to the field names are ok.

  6. #6
    Registered User
    Join Date
    03-28-2008
    Posts
    4

    Concatenate & If

    Hi Ben.

    Thanks for this. I have followed your notes, but I keep getting a VB error message - "Ambiguous name detected: MakeAddr" and the result "#NAME?" and I cannot see where it applies.

    Rgds,

    Ed.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Rather than parsing your formatting codes, you could list the code names across the top of a chart and use a look-up to pick which result to show. This also provides a way to add or modify different styles as the need arizes. The top row of the table (listing the formats) could feed a validation list for column D to prevent mis-typing.

    In the attached, the table in H:L could be hidden.

    I hope this helps.
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,919
    Ed,

    I don't know if you decided to use Mike's solution or not, but the ambiguous name error probably means that you have two functions or subs with the same name. Check all of your modules to make sure that you only have one function MakeAddr and make sure all spellings are correct (#name means that XL can't find the function).

    Ben

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1