Excel "master column" 重新组织行

Excel "master column" to reorganize rows

非常感谢任何帮助,

我正在尝试在 excel 中编译许多 table。我不想让左边的列表成为主列表,右边的列表根据匹配左边的C值排序。

(之前)

.....

(之后)

在 table 中唯一一致的变量是 C,它们应该完全匹配。棘手的事情是......当我将第二个 table 添加到 excel 时,它实际上是 "disorganized." 我希望立即重新组织它,以便 C 列将全面排列。变量"c"是一串字符和数字。

我稍后希望将 [a1] 与 [x1] 之类的东西进行比较,并让公式根据这些变量的差异创建一个新的 table

我不完全清楚你的实际情况,但也许你可以从这个插图版本中抄录。第 select 列 E:H 和 right-click,插入以将它们推到右侧。

据我所知,您的查找列位于要返回的值的右侧,因此 INDEX(MATCH()) 对比 VLOOKUP 更适合,因为后者要求查找列与要返回的值相同或在其左侧。

新E2中的公式是,

=IF(LEN($G2), INDEX(I:I, MATCH($C2,$K:$K, 0)), "")

这不会立即显示一个值,但稍后会修复。向右填一列然后把这个公式放到G2中,

=IFERROR(INDEX(K:K, MATCH($C2,$K:$K, 0)), "")

E2:G2 现在显示值(如果未找到 C2,则不显示),请根据需要填写。

好吧,我想出了一个解决方案,但是它使用 vba 而不是 excel 公式,因此它可能不适合您的需要

此函数的作用是查看左侧的列(您的主人)并从中读取值。然后它会在右侧(键列)中查找相同的文本。当它找到匹配项时,它会在两行之间交换数据

a1, b2, c1, x3, y3, c3  <--- starting with this row
a2, b2, c2, x1, y1, c1
a3, b3, c3, x2, y2, c2

所以在你的例子中我们正在寻找 c1 它在 行 2 中找到它所以它将第 1 行的值交易到行2 从第 2 行到第 1 行

a1, b2, c1, x1, y1, c1  <--- after swap
a2, b2, c2, x3, y3, c3  <--- after swap
a3, b3, c3, x2, y2, c2

它正在寻找下一次迭代 c2。它在 行 3 中找到它,因此它交换两行

a1, b2, c1, x1, y1, c1  
a2, b2, c2, x2, y2, c2  <--- after swap
a3, b3, c3, x3, y3, c3  <--- after swap

现在您的列表已排序!我尽力记录代码。只需更改顶部的值

Sub MoveDataRowsToMatchMasterList()
    data_sheet = "Sheet2"   'the name of the sheet with all the data
    mastercol = "c"         'this is the left hand side
    keycol = "f"            'this is the right hand side
    data_col_start = "d"    'this is the first data column on the right hand side
    data_col_end = keycol   'this is the last data column on the right hand side

    row_start = 2           'the first row of data
    row_end = 4             'the last row of data

    temp_sheet = "temporary_sheet" 'this is a temporary sheet that will store intermediate values
                                   'make sure the name doesnt match an existing sheet

    '---------------------- end of user configuration -------------------
    Application.StatusBar = "Sorting list, please be patient"
    Application.ScreenUpdating = False

    'adds temporary sheet
    Sheets.Add Before:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = temp_sheet

    'goes through all the rows of data
    For curr_row = row_start To row_end
        'initializes value
        row_with_match = 0

        'reads the value from the master column (left hand side)
        master_key = Sheets(data_sheet).Range(mastercol & curr_row).Value

        'searches all the rows until it finds value/key (from the right hand side)
        'that matches the master key
        'notice we start at the curr_row (we assume the rows above curr_row are
        'already sorted properly)
        For search_row = curr_row To row_end

            'reads the value from the key column (right hand side)
            curr_key = Sheets(data_sheet).Range(keycol & search_row).Value

            'if they match we found our key
            If StrComp(master_key, curr_key) = 0 Then
                row_with_match = search_row
                Exit For
            End If
        Next

        'MsgBox (master_key & " matched row " & row_with_match)

        'copies the data at the current row and puts it in the temporary sheet
        Sheets(data_sheet).Range(data_col_start & curr_row & ":" & data_col_end & curr_row).Copy
        Sheets(temp_sheet).Select
        Range(data_col_start & curr_row).Select
        Sheets(temp_sheet).Paste


        'copies the matching data and pastes it where it belongs
        Sheets(data_sheet).Range(data_col_start & row_with_match & ":" & data_col_end & row_with_match).Copy
        Sheets(data_sheet).Select
        Range(data_col_start & curr_row).Select
        Sheets(data_sheet).Paste

        'copies the data from the temporary sheet to where the match was found
        Sheets(temp_sheet).Range(data_col_start & curr_row & ":" & data_col_end & curr_row).Copy
        Sheets(data_sheet).Select
        Range(data_col_start & row_with_match).Select
        Sheets(data_sheet).Paste
    Next

    Application.DisplayAlerts = False
    'deletes temporary sheet
    Sheets(temp_sheet).Delete
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True
    Application.StatusBar = ""
End Sub

如果这解决了您的问题,请标记为解决方案