循环循环 - Vba

Loop in a Loop - Vba

我正在努力完成这个练习,因此我请求你的帮助。

我有一个 table 数据如下:

picture with data

我需要 2 个循环:

第一部分使用以下代码完成:

lastc = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
lastr = ws.Cells(ws.Rows.count, lastc).End(xlUp).Row
lastr2 = ws.Cells(ws.Rows.count, lastc - 2).End(xlUp).Row

For R = lastr To 2 Step -1
lastr = R

Set FindRow = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc))
FindRowNumber = FindRow.Row
Set CellPosition = Cells(FindRowNumber, lastc - 1)
Set FindRow2 = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
Find RowNumber2 = FindRow2.Row
Set CellPosition2 = Cells(FindRowNumber2, lastc - 1)

Next R

但我很难合并第二个循环并将结果移动到我想要的位置。

感谢您的提示。

我在你的代码中添加了操作来累加并输出结果:

Sub test1()
    Set ws = ActiveSheet
    
    ws.Columns("BZ").ClearContents  ' clear output column
    
    lastc = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    lastr = ws.Cells(ws.Rows.Count, lastc).End(xlUp).Row
    lastr2 = ws.Cells(ws.Rows.Count, lastc - 2).End(xlUp).Row
    
    Dim col As New Collection   'declare and create collection for results accumulation
    
    For R = lastr To 2 Step -1
        lastr = R
        
        Set FindRow = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc))
        FindRowNumber = FindRow.Row
        Set CellPosition = ws.Cells(FindRowNumber, lastc - 1)
        Set FindRow2 = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        FindRowNumber2 = FindRow2.Row
        Set CellPosition2 = ws.Cells(FindRowNumber2, lastc - 1)

        col.Add CellPosition2 - CellPosition    ' accumulate results in the collection
    Next R
    
    ' output previously accumulated results one-by-one
    cnt = 2 'skip header row
    For Each col_element In col
        ws.Cells(cnt, "BZ") = col_element
        cnt = cnt + 1
    Next
End Sub

In my opinion, an additional output loop is unnecessary, it would be more correct to output the results in the same line as the processed value in the "BX" column for a visual comparison of the processed value and the result

选项 2

Sub test2()
    Set ws = ActiveSheet
    
    ws.Columns("BZ").ClearContents  ' clear output column
    
    lastc = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    lastr = ws.Cells(ws.Rows.Count, lastc).End(xlUp).Row
    lastr2 = ws.Cells(ws.Rows.Count, lastc - 2).End(xlUp).Row
    
    For R = lastr To 2 Step -1
        lastr = R
        
        Set FindRow = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc))
        FindRowNumber = FindRow.Row
        Set CellPosition = ws.Cells(FindRowNumber, lastc - 1)
        Set FindRow2 = ws.Range(ws.Cells(1, lastc - 2), ws.Cells(lastr2, lastc - 2)).Find(What:=ws.Cells(R, lastc), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        FindRowNumber2 = FindRow2.Row
        Set CellPosition2 = ws.Cells(FindRowNumber2, lastc - 1)
    
        ws.Cells(R, "BZ") = CellPosition2 - CellPosition ' output results
    Next R
End Sub