dev*_*xer 72 microsoft-excel-2007 pivot-table microsoft-excel
我有看起来像这样的数据:
Id | Loc1 | Loc2 | Loc3 | Loc4
---+------+------+------+-----
1 | NY | CA | TX | IL
2 | WA | OR | NH | RI
Run Code Online (Sandbox Code Playgroud)
我想把它转换成这个:
Id | LocNum | Loc
---+--------+----
1 | 1 | NY
1 | 2 | CA
1 | 3 | TX
1 | 4 | IL
2 | 1 | WA
2 | 2 | OR
2 | 3 | NH
2 | 4 | RI
Run Code Online (Sandbox Code Playgroud)
在 Excel 2007 中执行此操作的最简单方法是什么?
Dav*_*llo 70
您可以使用数据透视表执行此操作。
您应该会看到一个包含数据透视表中所有数据的新工作表,并按照您要查找的方式进行了转置。
Datapig 技术提供了实际上比您需要的更复杂的分步说明- 他的示例仅转置了数据集的一部分并使用了与TextToColumns相结合的数据透视技术。但它确实有很多图片。
请注意,数据透视表将对数据进行分组。如果您希望取消分组,唯一的方法是复制数据透视表,并将“特殊粘贴”作为值。然后,您可以使用如下技术填充空白:http : //www.contextures.com/xlDataEntry02.html
小智 7
如果您的数据不是 Excel 数据透视表而只是数据,您可能需要使用一些简单的 VBA 代码“取消透视”它。代码取决于两个命名范围,源和目标。源是要取消透视的数据(不包括列/行标题,例如示例中的 NY-RI),目标是要放置结果的第一个单元格。
Sub unPivot()
Dim oTarget As Range
Dim oSource As Range
Dim oCell As Range
Set oSource = Names("Source").RefersToRange
Set oTarget = Names("Target").RefersToRange
For Each oCell In oSource
If oCell.Value <> "" Then
oTarget.Activate
' get the column header
oTarget.Value = oCell.Offset(-(oCell.Row - oSource.Row + 1), 0).Text
' get the row header
oTarget.Offset(0, 1).Value = oCell.Offset(0, _
-(oCell.Column - oSource.Column + 1)).Text
' get the value
oTarget.Offset(0, 2).Value = oCell.Text
' move the target pointer to the next row
Set oTarget = oTarget.Offset(1, 0)
End If
Next
Beep
End Sub
Run Code Online (Sandbox Code Playgroud)
我已经构建了一个加载项,可以让您这样做,并且可以轻松适应不同的情况。在这里查看:http : //tduhameau.wordpress.com/2012/09/24/the-unpivot-add-in/
Excel 2010中有一个相当不错的解决方案,只需要稍微使用一下数据透视表即可。
\n\n使用以下设置根据您的数据创建数据透视表:
\n\n 归档时间: |
|
查看次数: |
125722 次 |
最近记录: |