excel - 在另一个术语再次出现之后在 VBA 中查找第一次出现的术语

标签 excel vba format range

我正在尝试从已复制到 .xmlMS Excel 文档中提取信息。

结构如下(每行在 Excel 中的不同行):

<chapterid="chapter1">
    <value1>123</value1>
    <value2>456</value2>
</chapter>
<chapterid="chapter1">
    <value1>789</value1>
    <value2>012</value2>
</chapter>

我正在尝试指定 value1 (或 value2 )和 chapterid ,以在 value1 之后找到第一次出现的 <chapterid="chapter1">

(它还必须能够在不同的工作表中运行,因此是下面函数中的第二个变量。我在其中搜索的事件 Worksheet 称为 mysheet ,我现在正在尝试将结果放入此工作表中的 cells(2,2) 中。)

到目前为止,这是我的代码:
Sub extractreport()

Dim chapterid As String
Dim mysheet As String
Dim myvar as String

tradeid = "Chapter1"
mansheet = "Test2"
myvar = "value1"    

Worksheets(mysheet).Cells(2, 2).Value = FindContentAfter(chapterid, mysheet, myvar)

End Sub

我对 Function 的尝试如下:
Public Function FindContentAfter(chapterid As String, mysheet As String, myvar As String)

Dim foundcell As Range
Set wb = Workbooks("ThisWorkbook")
Dim rowfind As Range
Dim adjstring As String

adjstring = "<chapterid=" & Chr(34) & chapterid & Chr(34) & ">"
MsgBox adjstring 'searching for the beginning of the chapter, because the String "Chapter1" can occur in different `values` in other chapters.
Set rowfind = Worksheets(mysheet).Range("A:A").find(What:=adjstring, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False) 'find row in which chapter begins

Set foundcell = Worksheets(mysheet).Range("A:A").find(What:=mystring, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False, after:=Worksheets(mysheet).Range(rowfind)) 'find first myvar after the previously found row
If Not foundcell Is Nothing Then
    'MsgBox (mystring & " found in row: " & foundcell.Row)
Else
    'MsgBox (mystring & " not found")
End If
MsgBox "1    " & foundcell

FindContentAfter = Mid(foundcell.Value, InStr(foundcell.Value, ">") + 1, InStr(3, foundcell.Value, "<") - InStr(foundcell.Value, ">") - 1)

End Function

它适用于 adjstring 的定义和搜索。但是,找到 rowfind(作为本章的开头)并将其作为搜索 myvar 的起点是行不通的。

错误是“运行时错误 1004”。我尝试了各种方法,例如:
Set foundcell = Worksheets(mysheet).Range("A" & rowfind.Row() & ":A").find(What:=mystring, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)

,或 Range(rowfind).Row() ,但我无法解决这个问题。

最佳答案

如果我正确理解您的帖子,您正试图获取 <value1>123</value1> 之间的文本。 ,所以在你的情况下,你试图得到 123 .

如果这是正确的,您将需要将公式修改为以下公式:

FindContentAfter = Mid(foundcell.Value, InStr(foundcell.Value, ">") + 1, InStrRev(foundcell.Value, "<") - InStr(foundcell.Value, ">") - 1).

另外,在您的第二个 Find ,参数After正在寻找一个范围,所以在这种情况下,我们使用 rowfind Range 对象来自我们的第一个 Find .

尝试将您的代码修改为下面的代码,代码注释中的解释:
Option Explicit

Sub extractreport()

Dim chapterid As String, mysheet As String, myvar As String

chapterid = "Chapter1"
mysheet = "Test2"
myvar = "value1"

Worksheets(mysheet).Cells(2, 2).Value = FindContentAfter(chapterid, mysheet, myvar)

End Sub

'===================================================================================

Public Function FindContentAfter(chapterid As String, mysheet As String, myvar As String)

Dim foundcell As Range
Dim rowfind As Range
Dim adjstring As String

adjstring = "<chapterid=" & Chr(34) & chapterid & Chr(34) & ">"
MsgBox adjstring 'searching for the beginning of the chapter, because the String "Chapter1" can occur in different `values` in other chapters.

With Worksheets(mysheet)
    Set rowfind = .Range("A:A").Find(What:=adjstring, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False) ' find the range where the chapter begins
    ' conmfirm Chapter was found
    If rowfind Is Nothing Then
        MsgBox "Unable to find " & adjstring & " in worksheet " & .Name
        Exit Function
    End If

    Set foundcell = .Range("A:A").Find(What:=myvar, After:=rowfind, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False) 'find first myvar after the previously found row
    If Not foundcell Is Nothing Then
        MsgBox myvar & " found in row: " & foundcell.Row
    Else
        MsgBox myvar & " not found"
    End If
    MsgBox "1    " & foundcell
End With

FindContentAfter = Mid(foundcell.Value, InStr(foundcell.Value, ">") + 1, InStrRev(foundcell.Value, "<") - InStr(foundcell.Value, ">") - 1)

End Function

关于excel - 在另一个术语再次出现之后在 VBA 中查找第一次出现的术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57804788/

相关文章:

arrays - 使用数组加速 For 循环

sql-server - 数据流中的SSIS变量使用问题

正则表达式 - 如何测试 2 个 str 模式并根据哪个 str 模式匹配进行替换

excel - 在一系列单元格中获取 5 个最常见的分隔子字符串

excel - 将部分数组输出到范围而不循环?

java - 在 java 1.5 中截断 Float(不包括 setRoundingMode)

Android 数字格式不知何故错误,我得到的不是 3.5,而是 3.499999999,为什么?

c# - foreach 循环中的 String.Format 用于在同一行中打印

java - 如何在不设置 -Xmx 标志的情况下限制堆消耗

excel - Excel文件打开时的Matlab xlsread