英文:
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("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
I'm having problem returning the value at column G that is related to the match at column E and F
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()
'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
'Settings.
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)
'Covering each cell of RngData.
For Each RngCell In RngData
'Checking if RngCell contains both StrSearchWord01 and StrSearchWord02.
If InStr(1, RngCell.Value2, StrSearchWord01) <> 0 And InStr(1, RngCell.Value2, StrSearchWord02) <> 0 Then
'Setting DblIndex for the next row of results.
DblIndex = DblIndex + 1
'Reporting the values in VarResult (column 1 and 2).
VarResult(DblIndex, 1) = RngCell.Offset(0, -1).Value2
VarResult(DblIndex, 2) = RngCell.Value2
End If
'Checking if RngCell contains StrSearchWord03 and if VarResult hasn't already a value in the first and third column.
If InStr(1, RngCell.Value2, StrSearchWord03) <> 0 And _
VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 3) = "" And _
VarResult(Excel.WorksheetFunction.Max(DblIndex, 1), 1) <> "" _
Then
'Reporting the value in VarResult (column 3).
VarResult(DblIndex, 3) = RngCell.Value2
End If
Next
'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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论