在 Excel 2007 中将包含数据列的行转换为包含多行的列
我有一行数据,如下:
header1 header2 header3 header4 header5
row key datavalue1 datavalue2 datavalue3 datavalue4 datavalue5....
基本上,我有一个非规范化数据集,其中数据值逐行可能为空,也可能不为空。我需要使它们正常化。
即
12345678 NULL 10 3 NULL 14
会变成:
12345678 header2 10
12345678 header3 3
12345678 header5 14
我可以通过使用粘贴特殊转换来做到这一点,但我有数千行,我需要确保为每一行获得正确的行键。此外,每一行都有一堆与之相关的描述,我需要将其与每个数据值一起复制。
转换每一行列以使我拥有单列的多行且包含所有非空数据值加上关联的数据值引用的最简单方法是什么?我需要能够旋转数据集。
I have a row of data as follows:
header1 header2 header3 header4 header5
row key datavalue1 datavalue2 datavalue3 datavalue4 datavalue5....
so basically, I have a denormalized data set where the datavalues may or may not be empty on a row-by-row basis. I need to normalize them.
ie
12345678 NULL 10 3 NULL 14
would become:
12345678 header2 10
12345678 header3 3
12345678 header5 14
I could do this by using a paste special transform, but I have thousands of rows and I'd need to make sure that I get the right row key for each. furthermore, each row has a bunch of descriptives associated with it that I need copied over with each datavalue.
What is the easiest way to convert each row of columns such that I have multiple rows of a single column with all non-empty datavalues plus the associated datavalue reference? I need to be able to pivot the dataset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将创建一个循环遍历每一行并将数据输出到另一页的 VBA 宏。一旦数据输出,您就可以在新页面中创建数据透视表。
不确定您对 VBA 有多熟悉,但这可以很容易地通过将数据加载到数组(或对象集合,如果您确实想正确执行)中并将其写回来完成。
这是一个优秀的 VBA 文档的链接。
http://social.msdn。 microsoft.com/Forums/en/isvvba/thread/d712dbdd-c876-4fe2-86d2-7d6323b4262c
编辑
请注意,这并不是一个完全有效的解决方案,而是一个真正的通用框架帮助您朝正确的方向前进。
作为一个通用示例,它完成了您需要做的很多事情(不是最好的方法,但对于初学者来说可能是最简单的),这样的事情应该可以帮助您开始,尽管在没有看到更多工作表的情况下很难说。
I would create a VBA macro that loops through each row and output the data to another page. This would let you create your pivot table in the new page once the data has been outputed.
Not sure how familiar you are with VBA, but this could pretty easily be done by loading the data into an array (or collection of objects if you really want to do it correctly) and writing it back out.
Here is a link to a good VBA document.
http://social.msdn.microsoft.com/Forums/en/isvvba/thread/d712dbdd-c876-4fe2-86d2-7d6323b4262c
Edit
Please note this is not meant to be a fully working solution but really a generic framework to help you in the right direction.
As a generic example that does a lot of what you would need to do (not the best way, but probably the easiest for a beginer), something like this should get you started, although it is hard to say without seeing more of your worksheet.
如果您有五个“标题”列,请输入以下公式
复制 H1:J??并将特殊值粘贴到顶部。对 J 列进行排序并删除所有为零的内容。如果数据中有合法的零,那么您首先需要用一些唯一的字符串替换空白单元格,然后可以将其删除。
如果您有更多列,请将上述所有公式中的“5”替换为您拥有的任何数字。
If you have five "header" columns, enter these formulas
Copy H1:J?? and paste special values over the top. Sort on column J and delete anything that's a zero. If you have legitmate zeros in the data, then you first need to replace blank cells with some unique string that you can then delete later.
If you have more columns, then replace the '5' in all the above formulas with whatever number you have.
在我看来,您要做的部分工作是“取消透视”数据透视表。当我必须执行类似的任务时,我发现此提示非常有帮助: http://spreadsheetpage.com/index.php/tip/creating_a_database_table_from_a_summary_table/
请注意,在 Excel 2007 中,您可以使用按键 Alt+D、P 进入旧版 Excel 2003 数据透视表向导。
Seems to me that part of what you are trying to do is to "de-pivot" a pivot table. I've found this tip to be a tremendous help when I've had to do similar tasks: http://spreadsheetpage.com/index.php/tip/creating_a_database_table_from_a_summary_table/
Note that in Excel 2007, you can get to the old Excel 2003 pivot table wizard using the keystrokes Alt+D, P .
Excel 具有转置功能,可以满足您的需求。它相当隐蔽,而且有点笨拙,但可能比深入研究 VBA 更容易。以下是 Excel 2007 帮助的摘录:
例如,按列组织的区域销售数据在转置数据后会显示在行中,如下图所示。
1.在工作表上,执行以下操作:
要将数据从列重新排列到行,请选择包含数据的列中的单元格。
要将数据从行重新排列到列,请选择包含数据的行中的单元格。
2.在“开始”选项卡的“剪贴板”组中,单击“复制”。
键盘快捷键 要复制选定的数据,您还可以按 CTRL+C。
注意 您只能使用“复制”命令来重新排列数据。要成功完成此过程,请勿使用“剪切”命令。
3.在工作表上,选择要将复制的数据重新排列到的目标行或列的第一个单元格。
注意 复制区域(复制区域:当您要将数据粘贴到另一个位置时复制的单元格。复制单元格后,单元格周围会出现一个移动边框,表明它们已被复制。)和粘贴区域(粘贴区域:使用 Office 剪贴板剪切或复制的数据的目标位置不能重叠。确保您在粘贴区域中选择的单元格位于复制数据的区域之外。
4.在“开始”选项卡的“剪贴板”组中,单击“粘贴”下方的箭头,然后单击“转置”。
5.数据转置成功后,可以删除复制区的数据。
提示 如果转置的单元格包含公式,则公式会转置,并且对转置单元格中数据的单元格引用会自动调整。为了确保公式继续正确引用非转置单元格中的数据,请在转置公式之前使用绝对引用。
有关详细信息,请参阅在相对、绝对和混合引用之间切换。
Excel has a transpose feature which may address your needs. It's pretty hidden and a bit clumsy but likely easier than delving into VBA. Here's an excerpt from Excel 2007 Help:
For example, the regional sales data that is organized in columns appears in rows after transposing the data, as shown in the following graphics.
1.On the worksheet, do the following:
To rearrange data from columns to rows, select the cells in the columns that contain the data.
To rearrange data from rows to columns, select the cells in the rows that contain the data.
2.On the Home tab, in the Clipboard group, click Copy .
Keyboard shortcut To copy the selected data, you can also press CTRL+C.
Note You can only use the Copy command to rearrange the data. To complete this procedure successfully, do not use the Cut command.
3.On the worksheet, select the first cell of the destination rows or columns into which you want to rearrange the copied data.
Note Copy areas (copy area: The cells that you copy when you want to paste data into another location. After you copy cells, a moving border appears around them to indicate that they've been copied.) and paste areas (paste area: The target destination for data that's been cut or copied by using the Office Clipboard.) cannot overlap. Make sure that you select a cell in a paste area that falls outside of the area from which you copied the data.
4.On the Home tab, in the Clipboard group, click the arrow below Paste, and then click Transpose.
5.After the data is transposed successfully, you can delete the data in the copy area.
Tip If the cells that you transpose contain formulas, the formulas are transposed and cell references to data in transposed cells are automatically adjusted. To make sure that formulas continue to refer correctly to data in nontransposed cells, use absolute references in the formulas before you transpose them.
For more information, see Switch between relative, absolute, and mixed references.
让我们看一下 VBA 中可能的解决方案。我认为这真的很有帮助。以下是关于我的代码您应该了解的一些事项。
NULL
的值的代码。如果单元格为空,您需要检查If IsEmpty(rngCurrent.Value) Then
。'
Let's look at a possible solution in VBA. I think this will really help. Here are a few things you should know about my code.
NULL
. If the cell is empty, you'll want to check forIf IsEmpty(rngCurrent.Value) Then
instead.'