Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 创建自定义函数以从字符串中删除括号中的上下文_Vba_Excel - Fatal编程技术网

Vba 创建自定义函数以从字符串中删除括号中的上下文

Vba 创建自定义函数以从字符串中删除括号中的上下文,vba,excel,Vba,Excel,我想创建一个自定义函数,允许我从字符串中删除括号中的内容。假设我有一个字符串:“508(7S9 5DU)609(609)”,我想有一个函数,它接受这个字符串并转换成“508 609” 我一直在玩VBA编辑器,但我的VBA编程技能非常有限,更不用说不存在了 我提出了以下简洁的代码,但不幸的是,它不起作用: Function DelPar(Source As Range) Source.Replace What:="(*) ", Replacement:="", LookAt:=xlPart, S

我想创建一个自定义函数,允许我从字符串中删除括号中的内容。假设我有一个字符串:“508(7S9 5DU)609(609)”,我想有一个函数,它接受这个字符串并转换成“508 609”

我一直在玩VBA编辑器,但我的VBA编程技能非常有限,更不用说不存在了

我提出了以下简洁的代码,但不幸的是,它不起作用:

Function DelPar(Source As Range)

Source.Replace What:="(*) ", Replacement:="", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Function
当我输入“=DelPar(源引用)”时,我获得“0”作为值

提前感谢您的帮助

致以最良好的祝愿

此宏将首先计算有多少对
(“
)“””(*)“和
”*(“
{EDIT}添加了一个检查,如果没有
”(*)“
存在,则提前退出While循环,只有
”()
,并通过替换
工作表function.Max

然后,它为每一对循环一次,并构建一个以“
”(“
”)开头的字符串,一直运行到下一个“
”,并用“
”替换该字符串(即删除它)。如果它在到达“
”(“
”)之前碰到第二个“
”(“
”)
”,则它从“
”重新启动该字符串

Function ExciseBracketedText(ByVal Text As String, Optional TrimSpaces AS Boolean = False) As String
    Dim lBracketCount As Long, sExcise As String, lCounter As Long
    'How many pairs of Open/Close brackets are there?
    'lBracketCount = Len(Text) - WorksheetFunction.Max(Len(Replace(Text, "(", "")), Len(Replace(Text, ")", "")))
    'This does not distinguish between correct &  misordered pairs

    'New code works in VB environments other than Excel VBA
    lBracketCount = Len(Replace(Text, "(", ""))
    If lBracketCount < Len(Replace(Text, ")", "")) Then lBracketCount = Len(Replace(Text, ")", ""))
    lBracketCount = Len(Text) - lBracketCount

    '  e.g. ")()(" will return 2, even though only 1 will be trimmed
    ExciseBracketedText = Text
    sExcise = "" 'This is the text to cut out in each loop

    While lBracketCount > 0 'Once for each bracket pair
        For lCounter = 1 To Len(ExciseBracketedText)
            If Mid(ExciseBracketedText, lCounter, 1) = "(" Then
                sExcise = "(" 'Reset the text-to-excise every time we hit an open bracket
            ElseIf Mid(ExciseBracketedText, lCounter, 1) = ")" Then
                If Len(sExcise) > 0 Then
                    'Replace the text in brackets when we hit a close bracket
                    ExciseBracketedText = Replace(ExciseBracketedText, sExcise & ")", "")
                    Exit For
                End If
            ElseIf Len(sExcise) > 0 Then
                'If we already have an open bracket, at to the text we will remove
                sExcise = sExcise & Mid(ExciseBracketedText, lCounter, 1)
            End If
        Next lCounter
        lBracketCount = lBracketCount - 1 'Next pair
        sExcise = "" 'Reset the text to excise
        'If the first open bracket is AFTER the last close bracket, then we can exit early
        If InStr(ExciseBracketedText, "(") > InStrRev(ExciseBracketedText, ")") Then lBracketCount = 0
    Wend
    If TrimSpaces Then ExciseBracketedText=Application.Trim(ExciseBracketedText)
End Function
函数executeBracketedText(ByVal文本作为字符串,可选TrimSpaces作为Boolean=False)作为字符串
Dim LBRACKTCOUNT尽可能长,sExcise尽可能长,lCounter尽可能长
'有多少对开/闭括号?
'lBracketCount=Len(Text)-WorksheetFunction.Max(Len(替换(Text,“(”,”)),Len(替换(Text,“),”))
'这不区分正确和错误排列的对
'新代码在Excel VBA以外的VB环境中工作
lBracketCount=Len(替换(文本,(,))
如果lBracketCount0'一次
对于lCounter=1到Len(附加括号内的文本)
如果Mid(括号内的文本,l计数器,1)=”(“然后
sExcise=“(”)每次点击开放括号时,将文本重置为附加
ElseIf Mid(括号内的文本,l计数器,1)=“然后
如果Len(sExcise)>0,则
'当我们点击右括号时,替换括号中的文本
ExchangeBarketedText=替换(ExchangeBarketedText,六进制&“”,“”)
退出
如果结束
ElseIf Len(sExcise)>0则
'如果我们已经有一个开放的括号,那么我们将删除该文本的末尾
sExcise=sExcise&Mid(附加括号中的文本,l计数器,1)
如果结束
下一个计数器
lBracketCount=lBracketCount-1'下一对
sExcise=“”将文本重置为附加内容
“如果第一个打开的括号在最后一个关闭的括号之后,那么我们可以提前退出
如果InStr(execurebracketedtext,“(”)>InStrRev(execurebracketedtext,“)”),则lBracketCount=0
温德
如果是TrimSpaces,则ExchangeBarketedText=Application.Trim(ExchangeBarketedText)
端函数
此宏将首先计算有多少对
(“
)“
””(*)“和
”*(“
{EDIT}添加了一个检查,如果没有
”(*)“
存在,则提前退出While循环,只有
”()
,并通过替换
工作表function.Max

然后,它为每一对循环一次,并构建一个以“
”(“
”)开头的字符串,一直运行到下一个“
”,并用“
”替换该字符串(即删除它)。如果它在到达“
”(“
”)之前碰到第二个“
”(“
”)
”,则它从“
”重新启动该字符串

Function ExciseBracketedText(ByVal Text As String, Optional TrimSpaces AS Boolean = False) As String
    Dim lBracketCount As Long, sExcise As String, lCounter As Long
    'How many pairs of Open/Close brackets are there?
    'lBracketCount = Len(Text) - WorksheetFunction.Max(Len(Replace(Text, "(", "")), Len(Replace(Text, ")", "")))
    'This does not distinguish between correct &  misordered pairs

    'New code works in VB environments other than Excel VBA
    lBracketCount = Len(Replace(Text, "(", ""))
    If lBracketCount < Len(Replace(Text, ")", "")) Then lBracketCount = Len(Replace(Text, ")", ""))
    lBracketCount = Len(Text) - lBracketCount

    '  e.g. ")()(" will return 2, even though only 1 will be trimmed
    ExciseBracketedText = Text
    sExcise = "" 'This is the text to cut out in each loop

    While lBracketCount > 0 'Once for each bracket pair
        For lCounter = 1 To Len(ExciseBracketedText)
            If Mid(ExciseBracketedText, lCounter, 1) = "(" Then
                sExcise = "(" 'Reset the text-to-excise every time we hit an open bracket
            ElseIf Mid(ExciseBracketedText, lCounter, 1) = ")" Then
                If Len(sExcise) > 0 Then
                    'Replace the text in brackets when we hit a close bracket
                    ExciseBracketedText = Replace(ExciseBracketedText, sExcise & ")", "")
                    Exit For
                End If
            ElseIf Len(sExcise) > 0 Then
                'If we already have an open bracket, at to the text we will remove
                sExcise = sExcise & Mid(ExciseBracketedText, lCounter, 1)
            End If
        Next lCounter
        lBracketCount = lBracketCount - 1 'Next pair
        sExcise = "" 'Reset the text to excise
        'If the first open bracket is AFTER the last close bracket, then we can exit early
        If InStr(ExciseBracketedText, "(") > InStrRev(ExciseBracketedText, ")") Then lBracketCount = 0
    Wend
    If TrimSpaces Then ExciseBracketedText=Application.Trim(ExciseBracketedText)
End Function
函数executeBracketedText(ByVal文本作为字符串,可选TrimSpaces作为Boolean=False)作为字符串
Dim LBRACKTCOUNT尽可能长,sExcise尽可能长,lCounter尽可能长
'有多少对开/闭括号?
'lBracketCount=Len(Text)-WorksheetFunction.Max(Len(替换(Text,“(”,”)),Len(替换(Text,“),”))
'这不区分正确和错误排列的对
'新代码在Excel VBA以外的VB环境中工作
lBracketCount=Len(替换(文本,(,))
如果lBracketCount0'一次
对于lCounter=1到Len(附加括号内的文本)
如果Mid(括号内的文本,l计数器,1)=”(“然后
sExcise=“(”)每次点击开放括号时,将文本重置为附加
ElseIf Mid(括号内的文本,l计数器,1)=“然后
如果Len(sExcise)>0,则
'当我们点击右括号时,替换括号中的文本
ExchangeBarketedText=替换(ExchangeBarketedText,六进制&“”,“”)
退出
如果结束
ElseIf Len(sExcise)>0则
'如果我们已经有一个开放的括号,那么我们将删除该文本的末尾
sExcise=sExcise&Mid(附加括号中的文本,l计数器,1)
如果结束
下一个计数器
lBracketCount=lBracketCount-1'下一个pa
Public Function DELETE_BETWEEN_BRACKETS(ByRef ThisCell As Range) As String
If ThisCell.Count <> 1 Then
    DELETE_BETWEEN_BRACKETS = "ERROR: Only 1 cell allowed"
    Exit Function
End If

Dim ThisValue As String
Dim MyStart As Integer
Dim MyEnd As Integer

ThisValue = ThisCell.Value

MyStart = InStr(1, ThisValue, "(")
MyEnd = InStr(1, ThisValue, ")")

Do While MyStart > 0 And MyEnd > 0 And MyEnd > MyStart
    ThisValue = Left(ThisValue, MyStart - 1) & Right(ThisValue, Len(ThisValue) - MyEnd)
    MyStart = InStr(1, ThisValue, "(")
    MyEnd = InStr(1, ThisValue, ")")
Loop


DELETE_BETWEEN_BRACKETS = Application.WorksheetFunction.Trim(ThisValue) 'we delete double spaces

End Function