如何根据搜索字符串选择先行和后代行。- X

发布于 2024-11-08 23:03:31 字数 1139 浏览 2 评论 0原文

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

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

row1) 来源>应用程序名称1

row2) 目的地 >对应值1

row3) 目的地 >对应值2

第4行)来源>应用程序名称2

row5) 目的地>对应值3

第6行)来源>应用名称3

row7) 目的地>对应值1

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

我无法提供样本表,因为我的声誉点低于 10。

有没有人可以指导和帮助我。我已经花了 3 天的时间,但没有得到任何结果。 我有一个非常重要的时间表来准备这份库存表,我是手动完成的,手动完成需要 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 中的标准。

I'm not new to Excel VBA but not an expert too. I'm in strange problem, someone plz help me I'm not able to think anymore.

My Excel Story:
I have some 40,000 rows in a spreadsheet. rows are in the pattern mentioned as below:

row1) Source > AppName1

row2) Destination > corresponding value1

row3) Destination > corresponding value2

row4) Source > AppName2

row5) Destination > corresponding value3

row6) Source > AppName3

row7) Destination > corresponding value1

Now if search by AppName let's be AppName1 then row2 and row3 should be copied to next sheet along with row1.
If I search for Value1 then it should get row1, row2, row3 row7 and row6 should be copied to next sheet. That means search strings precedents and descendent's rows should be copied to next sheet.

I cannot provide the sample sheet as my reputation points are less than 10.

Is there anyone who can guide and assist me I have spent my 3 days in this but not got any result.
I have a very critical schedule for preparing this inventory sheet I was doing it manually and it was taking 5-6days to do manually. I thought of automizing it but got stuck.

Here is my code that is not working:

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

This code is not upto the mark still in WIP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-11-15 23:03:31

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

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

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

Your example data and requirements are difficult to understand.

I've modified your code a little, which may help you to progress.
If you can post a snippet of your data and the required result we can progress further

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
你对谁都笑 2024-11-15 23:03:31

在进行编码之前,让我们先解决这个问题......

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

假设所有段落都是三倍,所有标记“段落”开头的行具有相同的属性:行号模 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”的行,从那里开始进行行,直到再次达到包含“源”的行

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

before going to the coding, let's grab the problem ....

you want to search for anything in the sheet, and return the three rows that belong to the "paragraph" where your search landed

Under the assumption that ALL paragraphs are TRIPLES, all the rows that mark the beginning of a "paragraph" have the same property: rownumber modulo 3 has the same constant value. So in whatever rownumber your search lands, you need to go back until rownumber modulo 3 gets equal your constant value. Having arrived there, you play out 3 rows - and stop

now coding should become pretty simple .... you fire a search or place the cursor "somewhere" by other means, and fire the 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

Under the assumption that paragraphs are n-tuples AND contain the string "Source" at the first line, you can do something similar: Whereever your search lands, you go back row by row until you arrive at a row containing string "Source", from there you play out rows until you again reach a row containing "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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文