1

I am looking for some help converting my formulas to VBA code.

My data is currently in Column ($T10)

I currently have rows of data similar to:
Jane Doe (doe.jane@___.com)
JOHN DOE, SR (noemail-8858)
first second DE surname surname2 (email@_______.com)
first middle surname (email@_____.net)

Formulas to get the 'normal' names:

[First Surname]  =IF($C2678=1,(LEFT(B2684,SEARCH("(",B2684)-1)),"")    
[first name]     =IF($C4068=1,(LEFT(TRIM(B4074),FIND(" ",TRIM(B4074))-1)),"")
[middle name]    =IF($C3888=1,(IF(LEN(TRIM(B3894))-LEN(SUBSTITUTE(B3894," ",""))<>3,"",LEFT(MID(TRIM(B3894),FIND(" ",TRIM(B3894))+1,99),FIND(" ",MID(TRIM(B3894),FIND(" ",TRIM(B3894))+1,99))-1))),"")
[surname]        =IF($C4068=1,(TRIM(RIGHT(SUBSTITUTE(TRIM(LEFT(B4074,FIND("(",B4074)-1))," ",REPT(" ",99)),99))),"")  
[email]          =IF($C4068=1,(MID(TRIM(B4074),FIND("(",TRIM(B4074))+1,FIND(")",TRIM(B4074))-FIND("(",TRIM(B4074))-1)),"")  

Results (edited):

|  jane Doe       |  jane   |  middle  |  Doe      |  doe.jane@____.com  |    
|  first surname  |  first  |  middle  |  Surname  |  noemail-8858       |  

I've looked at both TRIM and SPLIT functions, however I haven't been able to find a way to split given the variables (, ( )) in the one cell.

I've used:
http://www.homeandlearn.org/left_and_right_functions.html

http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=269:excel-vba-string-functions-left-right-mid-len-replace-instr-instrrev&catid=79&Itemid=475

http://www.exceltrick.com/formulas_macros/vba-split-function/

They aren't really piecing together what I need. I can get some basics, but not the more complex formulas converted to VBA.

Many thanks in advance.

This is an extension of my previous enquiry in 2014, where I was able to get the formulas.
Excel 2010 search for text in IF function - separate cell data

3
  • The question you need to answer before anyone can start writing code is: How is Excel supposed to know if name2 in name1 name2 name3 is a middle name or a second surname? As in Hilary Rodham Clinton versus Jane Barbara Miller.
    – teylyn
    Feb 12, 2015 at 2:52
  • The reason to use VBA instead of the formulas is for the additional flexibility afforded by VBA. So the very first thing you need to do is devise the rules that you want to use in order to separate the name segments. Once you've accomplished that task, coding should be trivial. But devising clear and unambiguous rules for parsing names is not. Feb 12, 2015 at 3:16
  • Basically, you can't know, there is nothing in the data which stipulates which word is a surname or not. The formulas are designed to extract what I need. Anything that is flagged in the "middle" name will be data checked in the next step to see if it's a legit middle name, or a wayward surname1.
    – MrsAdmin
    Feb 12, 2015 at 3:16

1 Answer 1

2

Short of analysing exactly what your formulas are currently doing (i assume you're happy with how they work right?) then I can't see why you can't directly convert them all?

Start point:

=IF($C3888=1,(IF(LEN(TRIM(B3894))-LEN(SUBSTITUTE(B3894," ",""))<>3,"",LEFT(MID(TRIM(B3894),FIND(" ",TRIM(B3894))+1,99),FIND(" ",MID(TRIM(B3894),FIND(" ",TRIM(B3894))+1,99))-1))),"")

Formatted more:

=IF($C3888=1,
   (IF(LEN(TRIM(B3894))-LEN(SUBSTITUTE(B3894," ","")) <>3,
      "",
      LEFT(
         MID(
           TRIM(B3894),
           FIND(
              " ",
              TRIM(B3894)
           ) +1,
           99
         ),
         FIND(
            " ",
            MID(
               TRIM(B3894),
               FIND(
                   " ",
                   TRIM(B3894)
               )+1,
               99
            )
          )-1
         )
       )
      )
   ,"")

I think you've got a few too many Mids and Lefts than you need. This is how I've interpreted "Get the word between the first and second spaces of the trimmed value"... Is that right?

VBA-afied:

Function GetMiddleName(rgName As Range) As String
    Dim intTrimmed As Integer
    Dim intNoSpace As Integer
    Dim stTrimmed As String

    Dim intFirstSpace As Integer
    Dim intSecondSpace As Integer

    If rgName.Offset(-6, 1).Value = 1 Then ' This gives the "C3888" from the "B3894"
        stTrimmed = Trim(rgName.Value)
        intTrimmed = Len(stTrimmed)
        intNoSpace = Len(Replace(rgName.Value, " ", ""))

        If intTrimmed - intNoSpace <> 3 Then
            GetMiddleName = ""
            Exit Function
        Else
            intFirstSpace = InStr(1, stTrimmed, " ")
            intSecondSpace = InStr(intFirstSpace + 1, stTrimmed, " ")

            GetMiddleName = Mid(stTrimmed, intFirstSpace + 1, intSecondSpace - (intFirstSpace + 1))
            Exit Function
        End If
    Else
        GetMiddleName = ""
    End If
End Function

Hopefully that gets you started with some ideas for the other formulas... PS the "rept" formula = "string" in VBA (I didn't know there was a rept formula! Nice one!)

That gives me these results:

"Jane Doe (doe.jane@___.com)" = "" (fails the "len - nospaces <> 3" check)
"JOHN DOE, SR (noemail-8858)" = "DOE," (might wanna add a Replace(","...) )
"first second DE surname surname2 (email@_______.com)" = "" (fails the "<>3" check) 
"first middle surname (email@_____.net)" = "middle" Works Swimingly?
2
  • Thanks for your answer, I accepted it as it worked, turns out I also found an answer, I was able to convert my existing formulas in to their VBA counterpart, it's all a little messy, but it does work. Thanks for your time.
    – MrsAdmin
    Feb 17, 2015 at 4:12
  • 1
    yeah the advantage is readability for future revisions etc but sounds like a kind of "once off" exercise anyway :)
    – Simon
    Feb 17, 2015 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.