Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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
Excel VBA提取两个字符之间的字符串_Excel_Vba_String_Extract - Fatal编程技术网

Excel VBA提取两个字符之间的字符串

Excel VBA提取两个字符之间的字符串,excel,vba,string,extract,Excel,Vba,String,Extract,我试图使用字符@作为标记来提取字符串中两个出现的子字符串之间的文本。我知道有8次发生了@。我想循环遍历主字符串,并将子字符串写入工作表 虽然我为字符串textBetween提供了一个Dim表达式,但我得到了错误消息“error msg”对象变量或未设置块变量。我不明白为什么 代码来自,所以应该很简单,对吧?嗯,对我来说不是 我已经摆弄了几个小时,没有结果 Sub FindStrings() Dim sheet2 As Worksheet Dim i As Integer

我试图使用字符@作为标记来提取字符串中两个出现的子字符串之间的文本。我知道有8次发生了@。我想循环遍历主字符串,并将子字符串写入工作表

虽然我为字符串textBetween提供了一个Dim表达式,但我得到了错误消息“error msg”对象变量或未设置块变量。我不明白为什么

代码来自,所以应该很简单,对吧?嗯,对我来说不是

我已经摆弄了几个小时,没有结果

   Sub FindStrings()

   Dim sheet2 As Worksheet
   Dim i As Integer
   Dim openPos As Long
   Dim clsPos As Long
   Dim textBetween As String
   openPos = 0

   'Using for loop to find the i th occurrence of at '@' for openPos
   For i = 1 To 8

   'get position of start of string
    openPos = InStr(openPos + i, sheet2.Range("H8"), "@", vbTextCompare)     

    'Error msg "Object variable or With block variable not set

    'get position of end of string
    clsPos = InStr(openPos + 1 + i, sheet2.Range("H8"), "@", 
    vbTextCompare)  'End of string

   'get the mid string value between openPos and clsPos
   '
    textBetween = Mid(sheet2.Range("H8").Value, openPos + 1, clsPos - 
    openPos - 1)
    MsgBox ("textBetween  " & "i" & textBetween)

   'write to sheet
    sheet2.Cells(7 + i, 8).Value = textBetween

    Next i

    End Sub

我希望将字符串写入工作表。错误消息是:“error msg”Object variable或With block variable not set”

您需要设置/创建sheet2对象,然后才能使用它,即

Dim sheet2 as Worksheet

Set sheet2 = Sheets("Sheet2")

或者,如果已将VBE中的图纸名称引用从“图纸2”更改为“图纸2”,则不再需要将图纸2声明为工作表。

您需要先设置/创建图纸2对象,然后才能使用它,即

Dim sheet2 as Worksheet

Set sheet2 = Sheets("Sheet2")
Dim sheet2 As Worksheet
...
sheet2.Range("H8")
Option Explicit

Sub FindStrings()

    Dim i As Long
    Dim textBetween As String

    For i = 1 To 8

        textBetween = FindNthString(Sheet2.Range("H8").Value, i)

        'write to sheet
        Sheet2.Cells(8 + i, "H").Value = textBetween

    Next i

End Sub

Function FindNthString(str As String, ndx As Long, _
                       Optional delim As String = "@")

    FindNthString = CVErr(xlErrNA)

    'Split uses a zero-based array by default
    'the first 'piece' is at position 0
    ndx = ndx - 1

    If UBound(Split(str, delim)) >= ndx And ndx >= 0 Then

        FindNthString = Split(str, delim)(ndx)

    End If

End Function
或者,如果已将VBE中的图纸名称引用从“图纸2”更改为图纸2,则不再需要将图纸2声明为工作表

Dim sheet2 As Worksheet
...
sheet2.Range("H8")
Option Explicit

Sub FindStrings()

    Dim i As Long
    Dim textBetween As String

    For i = 1 To 8

        textBetween = FindNthString(Sheet2.Range("H8").Value, i)

        'write to sheet
        Sheet2.Cells(8 + i, "H").Value = textBetween

    Next i

End Sub

Function FindNthString(str As String, ndx As Long, _
                       Optional delim As String = "@")

    FindNthString = CVErr(xlErrNA)

    'Split uses a zero-based array by default
    'the first 'piece' is at position 0
    ndx = ndx - 1

    If UBound(Split(str, delim)) >= ndx And ndx >= 0 Then

        FindNthString = Split(str, delim)(ndx)

    End If

End Function
您已声明sheet2变量,但从未将其设置为工作表对象。巧合的是,工作簿中的第二个工作表具有可作为对象引用的。参考
sheet2.Range(“H8”)
将起作用,如果您打算参考该工作表;无需将尺寸表2声明为工作表。如果您的目的是引用另一个工作表,请不要使用sheet2,因为第二个工作表的代码名与表示集合对象的声明变量之间可能存在混淆。您还必须
将变量设置为工作表对象

'write to sheet
 sheet2.Cells(7 + i, 8).Value = textBetween
在For的第一次迭代过程中,上面将textBetween写入sheet2.Range(“H8”)
。。。下一个循环。随后的循环将重新读取覆盖的值,这样您的结果就不会是您期望的结果

最好的选择是将字符串拆分为一个从零开始的数组,然后选择要返回的片段。用户定义的函数可以在公用子系统中使用,也可以直接在工作表中使用

Dim sheet2 As Worksheet
...
sheet2.Range("H8")
Option Explicit

Sub FindStrings()

    Dim i As Long
    Dim textBetween As String

    For i = 1 To 8

        textBetween = FindNthString(Sheet2.Range("H8").Value, i)

        'write to sheet
        Sheet2.Cells(8 + i, "H").Value = textBetween

    Next i

End Sub

Function FindNthString(str As String, ndx As Long, _
                       Optional delim As String = "@")

    FindNthString = CVErr(xlErrNA)

    'Split uses a zero-based array by default
    'the first 'piece' is at position 0
    ndx = ndx - 1

    If UBound(Split(str, delim)) >= ndx And ndx >= 0 Then

        FindNthString = Split(str, delim)(ndx)

    End If

End Function

您已声明sheet2变量,但从未将其设置为工作表对象。巧合的是,工作簿中的第二个工作表具有可作为对象引用的。参考
sheet2.Range(“H8”)
将起作用,如果您打算参考该工作表;无需将尺寸表2声明为工作表。如果您的目的是引用另一个工作表,请不要使用sheet2,因为第二个工作表的代码名与表示集合对象的声明变量之间可能存在混淆。您还必须
将变量设置为工作表对象

'write to sheet
 sheet2.Cells(7 + i, 8).Value = textBetween
在For的第一次迭代过程中,上面将textBetween写入sheet2.Range(“H8”)
。。。下一个循环。随后的循环将重新读取覆盖的值,这样您的结果就不会是您期望的结果

最好的选择是将字符串拆分为一个从零开始的数组,然后选择要返回的片段。用户定义的函数可以在公用子系统中使用,也可以直接在工作表中使用

Dim sheet2 As Worksheet
...
sheet2.Range("H8")
Option Explicit

Sub FindStrings()

    Dim i As Long
    Dim textBetween As String

    For i = 1 To 8

        textBetween = FindNthString(Sheet2.Range("H8").Value, i)

        'write to sheet
        Sheet2.Cells(8 + i, "H").Value = textBetween

    Next i

End Sub

Function FindNthString(str As String, ndx As Long, _
                       Optional delim As String = "@")

    FindNthString = CVErr(xlErrNA)

    'Split uses a zero-based array by default
    'the first 'piece' is at position 0
    ndx = ndx - 1

    If UBound(Split(str, delim)) >= ndx And ndx >= 0 Then

        FindNthString = Split(str, delim)(ndx)

    End If

End Function

您有
Dim sheet2作为工作表
,但从不将工作表分配给该变量。您需要先设置/创建
sheet2
对象,然后才能使用它。如果只需要在
@
上拆分字符串,则
Dim arr:arr=split(sheet2.Range(“H8”).Value,“@”)
可以做到这一点。您有
Dim sheet2作为工作表
,但从不将工作表分配给该变量。您需要先设置/创建
sheet2
对象,然后才能使用它。如果您只需要在
@
上拆分字符串,则
Dim arr:arr=split(sheet2.Range(“H8”).Value,“@”)将执行此操作。