我正在寻找一些帮助将我的公式转换为VBA代码。
我的数据目前在专栏($ T10)
我目前的数据行类似于:
Jane Doe(doe.jane@___.com)
约翰DOE,SR(noemail-8858)
第一个第二个姓氏姓氏2(email@_______.com)
第一个中间姓(email@_____.net)
获取'普通'名称的公式:
[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 |
我查看了TRIM
和SPLIT
函数,但是我找不到在一个单元格中给定变量(, ( )
)的方法。
我用过:
http://www.homeandlearn.org/left_and_right_functions.html
http://www.exceltrick.com/formulas_macros/vba-split-function/
他们并没有拼凑出我需要的东西。我可以得到一些基础知识,但不是更复杂的公式转换为VBA。
非常感谢提前。
这是我在2014年的上一次调查的延伸,在那里我能够得到公式 Excel 2010 search for text in IF function - separate cell data
答案 0 :(得分:2)
如果没有准确分析您的公式目前正在做什么(我假设您对它们的工作原理感到满意吗?)那么我就不明白为什么您不能直接将它们全部转换?< / p>
起点:
=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
)
)
)
,"")
我认为你有太多的Mids and Lefts比你需要的要多。这就是我解释的方式&#34;在修剪后的值的第一个和第二个空格之间得到这个词&#34; ......是吗?
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
希望能让你开始为其他公式提出一些想法...... PS&#34; rept&#34; formula =&#34; string&#34;在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?