【问题标题】:VBA Extract Strings Between Two CharactersVBA 提取两个字符之间的字符串
【发布时间】:2019-10-10 12:07:13
【问题描述】:

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

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

代码来自excel vba- extract text between 2 characters,应该很简单吧?好吧,不适合我!

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

   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

我希望将字符串写入工作表。错误消息是:“错误消息“对象变量或未设置块变量”

【问题讨论】:

  • 您有Dim sheet2 As Worksheet,但从未将工作表分配给该变量。
  • 您需要先设置/创建sheet2对象才能使用它。
  • 如果您只需要在@ 上拆分字符串,那么Dim arr: arr = Split(sheet2.Range("H8").Value, "@") 会这样做。

标签: excel vba string extract


【解决方案1】:

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

Dim sheet2 as Worksheet

Set sheet2 = Sheets("Sheet2")

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

【讨论】:

    【解决方案2】:
    Dim sheet2 As Worksheet
    ...
    sheet2.Range("H8")
    

    您已经声明了 sheet2 变量,但从未将 Set 声明为工作表对象。巧合的是,工作簿中的第二个工作表有一个Worksheet.Codename property,可用作对象引用。如果您打算引用该工作表,则引用 sheet2.Range("H8") 将起作用;声明 Dim sheet2 As Worksheet 是不必要的。如果您的意图是引用另一个工作表,请不要使用 sheet2,因为第二个工作表的代号和表示集合对象的声明变量之间可能会混淆。您还必须 Set 将 var 转换为工作表对象。

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

    在 For ... Next 循环的第一次迭代期间,上面将 textBetween 写入 sheet2.Range("H8")。随后的循环重新读取覆盖的值,因此您的结果不会是您所期望的。

    您最好的选择是将字符串拆分为一个从零开始的数组并挑选出您想要返回的部分。 UserDefined 函数可以在公共子程序中使用,也可以直接在工作表上使用。

    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
    

    enter image description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2014-02-26
      • 1970-01-01
      • 2013-01-31
      • 2013-12-11
      • 2015-05-04
      相关资源
      最近更新 更多