获取Excel VBA中具有公共起始字和结束字的子字符串

时间:2013-06-14 04:36:22

标签: excel-vba vba excel

我有一个日志文件,我想要获取以<fullpath>"开头并以"结尾的子字符串。例如:

  

会员ID =“:LtWeek2:1133548”fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束02-24-13”levelName =“LtWeek2”rpmSetOperator =“Add” /会员ID =“:LtWeek2:1168692”fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束02-24-13”levelName =“LtWeek2”rpmSetOperator =“Add”/ Member ID =“:LtWeek2:1191834”fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束03-10-13”levelName =“LtWeek2”rpmSetOperator =“Add”

说,上述数据将出现在“A1”中。

现在我必须编写一个只有字符串的代码,例如

  

fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束02-24-13”   fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束02-24-13”   fullPath =“Time.Latest Weekly:FOLDER.Latest 52 Weeks结束04-21-13.Week结束03-10-13”

所以我的想法是,将一个起始单词作为“fullPath”并结束单词作为“levelName”,如果我们能够在两者之间获取文本,我的问题就可以解决了。

上述方法就在我身边,任何其他方法/方法也都可以。

1 个答案:

答案 0 :(得分:0)

以下代码读取Sheet1的单元格A1中的字符串,提取感兴趣的子字符串,并将它们写回到B,C和B列中的工作表中。 d。

  Sub ExtractFromLog()
      Dim str As String
      Dim entries() As String
      Dim numEntries As Long
      Dim i As Long
      Dim startPos As Long
      Dim endPos As Long

      str = Sheet1.Range("A1").Value
      numEntries = (Len(str) - Len(Replace(str, "fullPath", ""))) / Len("fullPath")
      ReDim entries(1 To numEntries)

      startPos = 1
      For i = 1 To numEntries
          startPos = InStr(startPos, str, "fullPath")
          endPos = InStr(startPos + 1, str, "levelName") - 1
          entries(i) = Mid(str, startPos, endPos - startPos + 1)
          startPos = endPos + 1
      Next i
      Sheet1.Range(Cells(1, 2), Cells(1, i)) = entries
  End Sub