Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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,为了得到第二个括号之后的值,我尝试使用我创建的这个括号,但是它不能正常工作,而且当我在每行中有更多的括号时,它变得更加复杂。。任何想法。我上传了几张图片来理解输入和输出 Sub stringtest() Dim text As String, i As Long, firstbracket As Long, secondbracket As Long Dim extractTest As String, y As Long y = 1 For i = 1 To

为了得到第二个括号之后的值,我尝试使用我创建的这个括号,但是它不能正常工作,而且当我在每行中有更多的括号时,它变得更加复杂。。任何想法。我上传了几张图片来理解输入和输出

Sub stringtest()

    Dim text As String, i As Long, firstbracket As Long, secondbracket As Long
    Dim extractTest As String, y As Long

    y = 1
    For i = 1 To 10

        text = Worksheets("Sheet1").Cells(i, 1).Value
        firstbracket = InStr(1, text, "[")
        secondbracket = InStr(firstbracket + 1, text, "]")
        extractTest = Mid(text, firstbracket + 1, secondbracket)

        Worksheets("Sheet1").Cells(y, 2).Value = extractTest
        y = y + 1

    Next i

End Sub

尝试按第二个括号拆分,然后使用
Left()
确定字符串的长度



使用
ArrayList

Dim rng As Range
Dim arl As Object
Dim strVal
Dim i As Long
Set arl = CreateObject("System.Collections.ArrayList")
For Each rng In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    strVal = Split(Replace(rng.Value, "[", "]"), "]")
    For i = 2 To UBound(strVal) Step 2
        arl.Add CStr(strVal(i))
    Next i
Next rng
For i = 0 To arl.Count - 1
    Range("B" & i + 1).Value = arl.Item(i)
Next i
Set arl = Nothing

这里有一个简短而快速的方法

Sub main()
    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        .Replace what:="[*]", replacement:="|", lookat:=xlPart
        .TextToColumns DataType:=xlDelimited, Other:=True, OtherChar:="|"
        .Columns(1).Delete xlToLeft
    End With
End Sub

这意味着您将获得
[?-??]
之后的所有值,对吗?请发布一些示例数据,而不是快照。您的示例输出似乎与您提供的代码不一致,因此我不清楚您到底想要什么。您可以通过为一些特定情况提供输入和输出的示例来解决这个问题。此外,如果数据的分隔正确,则可能不需要使用宏“数据-->文本”来解决此问题columns@L42是的,我想提取['''']示例[''''']之后的所有值…因此提取example@user3713336,你试过任何答案吗?这是最快最简单的:+1@user3713336:最新版本按行,然后按列顺序输出结果
Dim rng As Range
Dim arl As Object
Dim strVal
Dim i As Long
Set arl = CreateObject("System.Collections.ArrayList")
For Each rng In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    strVal = Split(Replace(rng.Value, "[", "]"), "]")
    For i = 2 To UBound(strVal) Step 2
        arl.Add CStr(strVal(i))
    Next i
Next rng
For i = 0 To arl.Count - 1
    Range("B" & i + 1).Value = arl.Item(i)
Next i
Set arl = Nothing
Sub main()
    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        .Replace what:="[*]", replacement:="|", lookat:=xlPart
        .TextToColumns DataType:=xlDelimited, Other:=True, OtherChar:="|"
        .Columns(1).Delete xlToLeft
    End With
End Sub