EXCEL 2010:使用 VBA 将单元格拆分为多个单元格

EXCEL 2010: Split Cells using VBA to multiple Cells

我正在寻求一些帮助,将我的公式转换为 VBA 代码。

我的数据目前在列 ($T10)

我目前的数据行类似于:
无名氏 (doe.jane@___.com)
JOHN DOE,SR (noemail-8858)
第一个第二个德姓 surname2 (email@_______.com)
第一个中间姓氏(电子邮件@_____.net)

获取 'normal' 名称的公式:

[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)),"")  

结果(已编辑):

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

我已经查看了 TRIMSPLIT 函数,但是我无法找到一种方法来拆分给定其中的变量 (, ( ))细胞.

我用过:
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/

他们并没有真正拼凑出我需要的东西。我可以得到一些基础知识,但不能转换为 VBA.

的更复杂的公式

非常感谢。

这是我之前在 2014 年的查询的扩展,在那里我可以获得公式。
Excel 2010 search for text in IF function - separate cell data

如果没有准确分析您的公式当前在做什么(我假设您对它们的工作方式感到满意,对吗?)那么我不明白为什么您不能直接将它们全部转换?

起点:

=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))),"")

格式化更多:

=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
         )
       )
      )
   ,"")

我认为您的中频和左频超出了您的需要。我是这样解释的 "Get the word between the first and second spaces of the trimmed value"... 对吗?

VBA-相关:

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

希望这能让你开始对其他公式产生一些想法...PS "rept" 公式 = "string" in VBA(我不知道有一个 rept 公式!不错!)

这给了我这些结果:

"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?