5

我有一个完整的电子表格,其中包含姓名和人员的角色,如下所示:

Role Name  Change
1     A     Yes
2     A     No
5     A     N/Ap
1     B     Yes
3     B     No
2     C     Yes
4     C     No

我必须想出一个如下所示的电子表格:

     1        2        3        4        5        6
A   Yes                     
B                       
C                       

基本上,它应该从第一个电子表格中检索信息,并在第二个电子表格中清楚地列出。

有太多的名称和角色需要手动完成。VLMOVE不会工作,我已经尝试过MATCHand INDEX

4

5 回答 5

2
于 2012-12-21T20:43:24.900 回答
2

@RocketDonkey 的替代方案(但感谢更完整的预期结果!)可能是将 Role 和 Name 串在一起(比如在 Sheet1 的 B 和 C 之间插入的列中[因为我认为 OP 想要一个单独的结果表]):

C2=A1&B2  copied down as required

然后在 Sheet2!B2 中使用查找:

=IFERROR(VLOOKUP(B$1&$A2,Sheet1!$C$2:$D$8,2,FALSE),"")  

根据需要上下复制。

这假设结果的网格(如问题)已构建(并且有 7 行数据 - 否则根据需要调整 8 美元。)

于 2012-12-21T21:48:02.163 回答
1

AMENDED

You can use array formulas to reorganize your table, without having to change the its structure. Assuming the data is in the range A2:C8 on Sheet1 and the result table is to be in range A1:G4 on Sheet2, the following formula would be the first entry (role 1 and name A) in the result table.

   =IFERROR(INDEX(Sheet1!$A$2:$C$8,MATCH(B$1&$A2,Sheet1!$A$2:$A$8&Sheet1!$B$2:$B$8,0),3),"-")

The MATCH formula returns the row number in which the role/name combination 1A occurs. The INDEX function returns the contents of the cell at the row number found by the MATCH formula and the column number 3, i.e., the Change column of your data table. The IFERROR returns "-" if the role/name combination is not in the data table.

Be sure to enter the formula using the Control-Shift-Enter key combination. Then copy the formula to the remaining cells of the result table.

The data table on Sheet1:

data table on Sheet1

The result table on Sheet2:

result table on Sheet

于 2012-12-21T22:19:24.667 回答
1

在没有 VBA 的情况下,还有另一种方法可以解决。如果您创建另一列连接第一个电子表格中的前两个列,如下所示:

Role Name  Change  CheckColumn
1     A     Yes    1A
2     A     No     2A
5     A     N/Ap   5A
1     B     Yes    1B
3     B     No     3B
2     C     Yes    2C
4     C     No     4C

然后您可以一起使用OffsetMatch函数来查找第二张表中的更改。因此,假设您的数据来自单元格 A1,则单元格 B2 中的公式为:

=iferror(offset(Sheet1!$A$1,match(B$1&$A2,sheet1!$D:$D,0),2),"")

或者,如果将连接列放在 sheet1 中的角色列之前,则可以vlookup在 sheet2 中使用,公式为:

=iferror(vlookup(B$1&$A2,sheet1!$A:$D,4,false),"")

于 2012-12-21T21:53:20.567 回答
1

好吧,既然有Excel-VBA标签,认为它会通过在 VBA 中添加一个来完成解决方案类型:) 下面的代码并不优雅,无论如何你需要使用代码库,试一试:)

代码:

Option Explicit

Public Sub sortAndPivot()
Dim d As Object
Dim ws As Worksheet
Dim sourceArray As Variant, pvtArray As Variant, v As Variant
Dim maxRole As Long
Dim i, j, k, m As Integer

    Set d = CreateObject("Scripting.Dictionary")
    Set ws = Worksheets("Sheet3") '-- set according to your sheet
    '-- you could enhance by using an input box to select the range
    sourceArray = Application.WorksheetFunction.Transpose(ws.Range("B3:D9").Value)
    '-- max role number
    maxRole = Application.WorksheetFunction.Max(ws.Range("B3:B9"))

    '-- find unique name list
    For i = LBound(sourceArray, 2) To UBound(sourceArray, 2)
        If Not d.exists(sourceArray(2, i)) Then
            d.Add sourceArray(2, i), i
        End If
    Next i

    ReDim pvtArray(d.Count, maxRole)
    pvtArray(0, 0) = "Name"

    '-- add unique names from dictionary
    j = 1
    For Each v In d.keys
        pvtArray(j, 0) = v
        j = j + 1
    Next

    '-- add unique Role number list
    For i = UBound(pvtArray, 2) To LBound(pvtArray) + 1 Step -1
        pvtArray(0, i) = i
    Next i

    '-- sort into the correct positions
    For k = LBound(pvtArray, 1) + 1 To UBound(pvtArray, 1)
        For m = LBound(pvtArray, 2) + 1 To UBound(pvtArray, 2)
            For i = LBound(sourceArray, 2) To UBound(sourceArray, 2)
                If pvtArray(k, 0) = sourceArray(2, i) Then
                    If pvtArray(0, m) = sourceArray(1, i) Then
                        pvtArray(k, m) = sourceArray(3, i)
                    End If
            End If
        Next i
        Next m
    Next k

    'Output the processed array into the Sheet in pivot view.
    Range("F2").Resize(UBound(pvtArray) + 1, _
    UBound(Application.Transpose(pvtArray))) = pvtArray

    Set d = Nothing
End Sub

结果:

在此处输入图像描述

于 2012-12-22T00:25:55.263 回答