after the matched cell, search in the succeeding rows for a value with specific string and return the value

huangapple go评论61阅读模式
英文:

after the matched cell, search in the succeeding rows for a value with specific string and return the value

问题

Here's the translation of the provided content:

"请问如何在匹配的单元格之后的后续行中搜索具有特定字符串的值,并将该值与第一个匹配的单元格一起返回?

以下是我的代码示例:

Sub test()
x = 1
For Line = 2 To Range("B" & Rows.Count).End(xlUp).Row

    If InStr(1, Range("B" & Line), "Main") <> 0 And InStr(1, Range("B" & Line), "Sub") <> 0 Then
    x = x + 1
    Range("F" & x) = Range("B" & Line)
    Range("E" & x) = Range("A" & Line)
    Range("G" & x) = Range("B" & Line)
    End If
Next

End Sub

我遇到了一个问题,无法返回与列E和F中的匹配相关的列G中的值。

以下是代码应该执行的示例。"

(Note: I have provided the translated code and content without answering your request to not answer translation questions, as the content you provided appears to be code-related.)

英文:

May I ask how to search in the succeeding rows after matched cell, for a value with specific string and return the value along with the first matched cell

here is the sample of my code

Sub test()
x = 1
For Line = 2 To Range(&quot;B&quot; &amp; Rows.Count).End(xlUp).Row

    If InStr(1, Range(&quot;B&quot; &amp; Line), &quot;Main&quot;) &lt;&gt; 0 And InStr(1, Range(&quot;B&quot; &amp; Line), &quot;Sub&quot;) &lt;&gt; 0 Then
    x = x + 1
    Range(&quot;F&quot; &amp; x) = Range(&quot;B&quot; &amp; Line)
    Range(&quot;E&quot; &amp; x) = Range(&quot;A&quot; &amp; Line)
    Range(&quot;G&quot; &amp; x) = Range(&quot;B&quot; &amp; Line).
    End If
Next

End Sub

I'm having problem returning the value at column G that is related to the match at column E and F

after the matched cell, search in the succeeding rows for a value with specific string and return the value

here is the sample of what should the code must do

答案1

得分: 1

以下是您提供的代码的翻译部分:

尝试这段代码

    Option Explicit
    Sub SubDataExtraction()
        
        '声明部分。
        Dim DblIndex As Double
        Dim VarResult() As Variant
        Dim RngResult As Range
        Dim RngCell As Range
        Dim RngData As Range
        Dim StrSearchWord01 As String
        Dim StrSearchWord02 As String
        Dim StrSearchWord03 As String
        
        '设置部分。
        Set RngData = Range(Range("B2"), Range("B" & Range("B" & Rows.Count).End(xlUp).Row))
        Set RngResult = Range("E2")
        StrSearchWord01 = "Main"
        StrSearchWord02 = "Sub"
        StrSearchWord03 = "animal"
        ReDim VarResult(1 To RngData.Rows.Count, 1 To 3)
        
        '遍历RngData中的每个单元格。
        For Each RngCell In RngData
            
            '检查RngCell是否同时包含StrSearchWord01和StrSearchWord02。
            If InStr(1, RngCell.Value2, StrSearchWord01) <> 0 And InStr(1, RngCell.Value2, StrSearchWord02) <> 0 Then
                
                '设置DblIndex以便下一行结果。
                DblIndex = DblIndex + 1
                
                '报告VarResult中的值(第1列和第2列)。
                VarResult(DblIndex, 1) = RngCell.Offset(0, -1).Value2
                VarResult(DblIndex, 2) = RngCell.Value2
                
            End If
            
            '检查RngCell是否包含StrSearchWord03,以及VarResult第1列和第3列是否还没有值。
            If InStr(1, RngCell.Value2, StrSearchWord03) <> 0 And _
               VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 3) = "" And _
               VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 1) <> "" _
               Then
                
                '报告VarResult中的值(第3列)。
                VarResult(DblIndex, 3) = RngCell.Value2
                
            End If
            
        Next
        
        '将结果报告在适当扩展的RngResult中。
        RngResult.Resize(DblIndex, UBound(VarResult, 2)).Value2 = VarResult
        
    End Sub

请注意,我已将代码中的注释进行了翻译,但未包括在代码部分中。如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

Try this code:

Option Explicit
Sub SubDataExtraction()
    
    &#39;Declarations.
    Dim DblIndex As Double
    Dim VarResult() As Variant
    Dim RngResult As Range
    Dim RngCell As Range
    Dim RngData As Range
    Dim StrSearchWord01 As String
    Dim StrSearchWord02 As String
    Dim StrSearchWord03 As String
    
    &#39;Settings.
    Set RngData = Range(Range(&quot;B2&quot;), Range(&quot;B&quot; &amp; Range(&quot;B&quot; &amp; Rows.Count).End(xlUp).Row))
    Set RngResult = Range(&quot;E2&quot;)
    StrSearchWord01 = &quot;Main&quot;
    StrSearchWord02 = &quot;Sub&quot;
    StrSearchWord03 = &quot;animal&quot;
    ReDim VarResult(1 To RngData.Rows.Count, 1 To 3)
    
    &#39;Covering each cell of RngData.
    For Each RngCell In RngData
        
        &#39;Checking if RngCell contains both StrSearchWord01 and StrSearchWord02.
        If InStr(1, RngCell.Value2, StrSearchWord01) &lt;&gt; 0 And InStr(1, RngCell.Value2, StrSearchWord02) &lt;&gt; 0 Then
            
            &#39;Setting DblIndex for the next row of results.
            DblIndex = DblIndex + 1
            
            &#39;Reporting the values in VarResult (column 1 and 2).
            VarResult(DblIndex, 1) = RngCell.Offset(0, -1).Value2
            VarResult(DblIndex, 2) = RngCell.Value2
            
        End If
        
        &#39;Checking if RngCell contains StrSearchWord03 and if VarResult hasn&#39;t already a value in the first and third column.
        If InStr(1, RngCell.Value2, StrSearchWord03) &lt;&gt; 0 And _
           VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 3) = &quot;&quot; And _
           VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 1) &lt;&gt; &quot;&quot; _
           Then
            
            &#39;Reporting the value in VarResult (column 3).
            VarResult(DblIndex, 3) = RngCell.Value2
            
        End If
        
    Next
    
    &#39;Reporting the result in RngResult properly expanded.
    RngResult.Resize(DblIndex, UBound(VarResult, 2)).Value2 = VarResult
    
End Sub

答案2

得分: 0

Here is the translated code without the code parts:

Sub test2()

Dim x As Long, Line As Long, SubLine As Long

x = 1
For Line = 2 To Range("B" & Rows.Count).End(xlUp).Row

    If InStr(1, Range("B" & Line), "Main") <> 0 And InStr(1, Range("B" & Line), "Sub") <> 0 Then
        
        SubLine = Line
        Do Until Range("A" & SubLine) <> Range("A" & SubLine + 1)
            SubLine = SubLine + 1
        Loop
        
        x = x + 1
        Range("E" & x) = Range("A" & Line)
        Range("F" & x) = Range("B" & Line)
        Range("G" & x) = Range("B" & SubLine)
    End If
Next

End Sub

Please note that this code appears to be VBA code for Excel.

英文:
Sub test2()

Dim x As Long, Line As Long, SubLine As Long

x = 1
For Line = 2 To Range(&quot;B&quot; &amp; Rows.Count).End(xlUp).Row

    If InStr(1, Range(&quot;B&quot; &amp; Line), &quot;Main&quot;) &lt;&gt; 0 And InStr(1, Range(&quot;B&quot; &amp; Line), &quot;Sub&quot;) &lt;&gt; 0 Then
        
        SubLine = Line
        Do Until Range(&quot;A&quot; &amp; SubLine) &lt;&gt; Range(&quot;A&quot; &amp; SubLine + 1)
            SubLine = SubLine + 1
        Loop
        
        x = x + 1
        Range(&quot;E&quot; &amp; x) = Range(&quot;A&quot; &amp; Line)
        Range(&quot;F&quot; &amp; x) = Range(&quot;B&quot; &amp; Line)
        Range(&quot;G&quot; &amp; x) = Range(&quot;B&quot; &amp; SubLine)
    End If
Next

End Sub

huangapple
  • 本文由 发表于 2023年4月17日 01:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定