【问题标题】:How to pick precedents and descendents rows on the basis of search string.- X如何根据搜索字符串选择先例行和后代行。- X
【发布时间】:2011-05-20 22:18:58
【问题描述】:

我不是 Excel VBA 新手,但也不是专家。我遇到了奇怪的问题,请有人帮助我,我无法再思考了。

我的 Excel 故事: 我在电子表格中有大约 40,000 行。行的模式如下:

row1) 来源 > AppName1

row2) 目的地> 对应值1

row3) 目的地> 对应值2

row4) 来源 > AppName2

row5) 目的地> 对应值3

row6) 来源 > AppName3

row7) 目的地> 对应值1

现在,如果按 AppName 搜索让我们成为 AppName1,则 row2 和 row3 应与 row1 一起复制到下一张表。 如果我搜索 Value1,那么它应该得到 row1、row2、row3、row7 和 row6 应该被复制到下一张表。这意味着搜索字符串的先例和后代的行应复制到下一张表。

由于我的声望点低于 10,我无法提供样本表。

有没有人可以指导和帮助我我花了三天时间但没有得到任何结果。 我有一个非常关键的时间表来准备这份清单,我是手动完成的,手动完成需要 5-6 天。我想自动化它,但被卡住了。

这是我的代码不起作用:

Sub GenerateInventory()
On Error GoTo ErrHandler:
Set r = ActiveSheet.UsedRange
nLastRow = r.Rows.Count + r.Row - 1
Set r1 = Cells(2, 8)
For i = 2 To nLastRow Step 1
If InStr(Cells(i, 6), "CMRI") <> 0 Then
Set r1 = Union(r1, Cells(i, 1))
End If
Next
r1.EntireRow.Select
r1.EntireRow.Copy
Sheets("MS4Inventory").Select
Cells(100, 1).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Exit Sub
ErrHandler:
MsgBox Err.Number & ": " & Error.Description

End Sub

此代码仍处于 WIP 中。

【问题讨论】:

  • 这3天你尝试了什么?让我们知道,否则我们无法帮助您更正它...
  • 你的例子比较难理解。请向我们展示您的工作表样本,并正确格式化,例如code 获取固定宽度的字体。
  • 您不需要 10 声望来正确格式化您的示例。只需将您的工作表示例写为文本,将其格式化为code,并使用空格等正确对齐行和列。我仍然不明白您的工作表是什么样的。单元格 A2 是否真的包含文本 "Destination &gt; corresponding value1"

标签: excel excel-2007 vba


【解决方案1】:

您的示例数据和要求难以理解。

我对您的代码进行了一些修改,这可能会帮助您取得进步。
如果您可以发布您的数据和所需结果的 sn-p,我们可以进一步取得进展

Sub GenerateInventory()
    Dim r As Range, r1 As Range, rMS4Inventory As Range
    Dim nLastRow As Long, i As Long
    Dim wb As Workbook, sh As Worksheet, shMS4Inventory As Worksheet

    On Error GoTo ErrHandler:

    Set wb = ActiveWorkbook
    Set sh = wb.ActiveSheet
    Set shMS4Inventory = wb.Worksheets("MS4Inventory")

    Set r = sh.UsedRange
    nLastRow = r.Rows.Count + r.Row - 1
    Set r1 = sh.Cells(2, 8)
    For i = 2 To nLastRow Step 1
        If InStr(sh.Cells(i, 6), "CMRI") <> 0 Then
            Set r1 = Union(r1, sh.Cells(i, 1))
        End If
    Next
    Set rMS4Inventory = shMS4Inventory.Cells(100, 1).End(xlUp).Offset(1, 0).EntireRow
    r1.EntireRow.Copy rMS4Inventory
Exit Sub
ErrHandler:
    Resume
    MsgBox Err.Number & ": " & Error.Description

End Sub

【讨论】:

    【解决方案2】:

    在开始编码之前,让我们抓住问题......

    您想在工作表中搜索任何内容,并返回属于您的搜索所在“段落”的三行

    假设所有段落都是 TRIPLES,所有标记“段落”开头的行都具有相同的属性:rownumber 模 3 具有相同的常数值。因此,在您搜索的任何行号中,您需要返回直到行号模 3 等于您的常数值。到达那里后,你玩了 3 行 - 然后停下来

    现在编码应该变得非常简单了......你触发搜索或通过其他方式将光标放在“某处”,然后触发Sub Grab()

    Sub Grab3Rows()
    Dim Idx As Long
        Idx = Selection.Row
    
        'find start of paragraph
        Do While Idx Mod 3 <> 2 ' change this constant as per your sheet
            Idx = Idx - 1
        Loop
    
        'select the 3 cells at the start of paragraph
        Selection.Offset(Idx - Selection.Row, 0).Resize(3, 1).Select
    
        'do the rest
    End Sub
    

    假设段落是 n 元组并且在第一行包含字符串“Source”,您可以执行类似的操作:无论您的搜索落在哪里,您都会逐行返回,直到到达包含字符串“ Source”,从那里播放行,直到再次到达包含“Source”的行

    Sub GrabByTextString()
    Dim Idx As Long
        Idx = Selection.Row
    
        'find start of paragraph
        Do While Left(Selection.Offset(Idx - Selection.Row, 0), 6) <> "Source"
            Idx = Idx - 1
        Loop
    
        'select the the start of paragraph
        Selection.Offset(Idx - Selection.Row, 0).Select
    
        'expand selection until we reach next paragraph start
        Idx = 1
    
        Do While Left(Selection(1, 1).Offset(Idx, 0), 6) <> "Source"
            Idx = Idx + 1
            Selection.Resize(Idx, 1).Select
        Loop
    
        'do the rest
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2013-05-17
      • 1970-01-01
      • 2021-12-25
      • 2018-10-04
      • 2023-03-09
      相关资源
      最近更新 更多