Excel“主列”以重新组织行

时间:2015-01-04 22:55:12

标签: excel excel-formula metadata spreadsheet

非常感谢任何帮助,

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

(前)

  • [a1] [b1] [ c1 ] - [x3] [y3] [c3]
  • [a2] [b2] [ c2 ] - [x1] [y1] [c1]
  • [a3] [b3] [ c3 ] - [x2] [y2] [c2]

.....

(后)

  • [a1] [b1] [ c1 ] - [x1] [y1] [ c1 ]
  • [a2] [b2] [ c2 ] - [x2] [y2] [ c2 ]
  • [a3] [b3] [ c3 ] - [x3] [y3] [ c3 ]

表中唯一一致的变量是C,它们应该完全匹配。棘手的是......当我将第二个表添加到excel时,它基本上是“混乱的”。我希望立即对其进行重新组织,以便C栏将全面排列。变量“c”是一串字符和数字。

我后来希望比较像[a1]到[x1]之类的东西,并根据这些变量的差异创建一个新的表格

2 个答案:

答案 0 :(得分:0)

我并不完全清楚你的实际情况,但也许你可以从这个插图版本转录。首先选择E:H列并单击鼠标右键,然后单击“插入”将其推向右侧。

Reorder Data

正如我所看到的那样,您的查阅列位于要返回的值的右侧,因此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则不显示),必要时填写。

答案 1 :(得分:0)

我想出了一个解决方案,但是它使用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

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