我有以下公式要转换为VBA中的函数。
=IFERROR(LEFT(B1,(FIND(",",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B1,"(",","),"-",","),"/",","))-1)),SUBSTITUTE(B1,".", ""))
我尝试过一些事情,但却无法让它正常运作。 Test
Function CharacterSwap(text)
Dim Old As Variant
Dim Comma As Variant
Old = Array(")", "(", "-", "/")
Comma = Array(",", ",", ",", ",")
For i = LBound(Old) To UBound(Comma)
If InStr(1, text, Old(i), vbBinaryCompare) > 0 Then
CharacterSwap = Replace(text, Old(i), Comma(i), 1, 1, vbBinaryCompare)
End If
Next i
End Function
Function SCRUB(text)
If InStr(1, CharacterSwap(text), ",", vbBinaryCompare) > 0 Then
SCRUB = Left((CharacterSwap(text)), InStr(1, (CharacterSwap(text)), ",") - 1)
Else
SCRUB = text
End If
End Function
答案 0 :(得分:1)
您也可以使用正则表达式在VBA中完成此操作。
如果我理解正确,您希望用逗号替换字符串中()/-
集合中的任何/所有字符。
Option Explicit
Function CharacterSwap(S As String) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "[()/-]"
.Global = True
CharacterSwap = .Replace(S, ",")
End With
End Function
如果你碰巧在上面.Pattern
的角色类中添加了字符,你应该知道破折号-
必须是该类中列出的第一个或最后一个字符。
如果它在其他任何地方,它将被解释为表示由前后字符限定的字符范围。
换句话说
[0-9]
将包含所有数字。 [-09]
仅包含dash
,0
和9
答案 1 :(得分:0)
rotate_left_byte_by1:
movl %edi, %eax
rolb %al
ret
但是这个简单的函数可以用更少的行来完成所有这些:
Function CharacterSwap(text)
Dim Old As Variant
Dim Comma As Variant
Old = Array(")", "(", "-", "/")
Comma = Array(",", ",", ",", ",")
For i = LBound(Old) To UBound(Old)
If InStr(1, text, Old(i), vbBinaryCompare) > 0 Then
'adjust the text in the text variable to maintain the changes
text = Replace(text, Old(i), Comma(i), 1, 1, vbBinaryCompare)
End If
Next i
CharacterSwap = text
End Function
Function SCRUB(text)
Dim newtext As String
'put output into a variable to avoid the multiple calls
newtext = CharacterSwap(text)
If InStr(1, newtext, ",", vbBinaryCompare) > 0 Then
SCRUB = Left(newtext, InStr(1, newtext, ",") - 1)
Else
SCRUB = text
End If
End Function