将Excel行转换为列
如何将A列中的行转换为列,将B列中的行转换为单行 以下是我当前的设置:将Excel行转换为列,excel,csv,pivot,pivot-table,Excel,Csv,Pivot,Pivot Table,如何将A列中的行转换为列,将B列中的行转换为单行 以下是我当前的设置: ColumnA ColumnB Question1 Answer1 Question2 Answer2 Question3 Answer3 Question4 Answer4 Question5 Answer5 Question1 Answer6 Question2 Answer7 Question3 Answer8 Question4 Answer9 Qu
ColumnA ColumnB
Question1 Answer1
Question2 Answer2
Question3 Answer3
Question4 Answer4
Question5 Answer5
Question1 Answer6
Question2 Answer7
Question3 Answer8
Question4 Answer9
Question5 Answer10
我想要的是:
Question1 Question2 Question3 Question4 Question5
Answer1 Answer2 Answer3 Answer4 Answer5
Answer6 Answer7 Answer8 Answer9 Answer10
我认为最好用VBA宏来解决这个问题。如果您在
Sheet1
中获得了当前表格,并希望将其转换为Sheet2
,则以下宏将执行转换
选项显式
子行()
将ws1标注为工作表,将ws2标注为工作表
尺寸i为整数,j为整数
设置ws1=ActiveWorkbook.Sheets(“Sheet1”)
设置ws2=ActiveWorkbook.Sheets(“Sheet2”)
'列出第1列中的每个值
对于i=1到ws1.Cells(Rows.Count,1)。结束(xlUp)。行步骤1
对于j=1到ws2.Cells(1,Columns.Count).End(xlToLeft).Column+1步骤1
如果ws2.Cells(1,j).Value=ws1.Cells(i,1).Value,则退出
如果ws2.Cells(1,j).Value=vbNullString,则
ws2.Cells(1,j).Value=ws1.Cells(i,1).Value
退出
如果结束
下一个j
接下来我
'使用匹配值填充列
对于i=1到ws1.Cells(Rows.Count,1)。结束(xlUp)。行步骤1
对于j=1到ws2.Cells(1,Columns.Count)。结束(xlToLeft)。列步骤1
如果ws2.Cells(1,j).Value=ws1.Cells(i,1).Value,则
ws2.Cells(Rows.Count,j).End(xlUp).Offset(1,0).Value=ws1.Cells(i,2).Value
如果结束
下一个j
接下来我
端接头
非VBA方法:
如果您正在处理一次性问题,处理从列到行的转换的简单方法是使用INDEX()函数。我建议在第一个目标单元格中使用以下内容(对于要放置“Question1”和“Question2”的位置):
从这里开始,只需将方程复制到右侧,直到您跨越的列数与行数相等,因为这允许列到行的转换无限期地向右扩展
**只要您的问答数据位于指定的A列和B列,并且可以相应地进行修改(使用列(A1)的另一种方法来定位您正在移动的行),此操作就有效
而且,如果您想在完成上面显示的5列后将问题和答案堆叠起来,请按如下方式设置:
Question1 Destination Cell:
=INDEX($A:$A,COLUMN(A1))
Answer1 Destination Cell:
=INDEX($B:$B,COLUMN(A1))
Answer6 Destination Cell:
=INDEX($B:$B,MATCH([non-locked cell address of Answer1],$B:$B,0)+5)
从那里,将这三个单元格复制到右侧的5列。然后,要向下展开,只复制以Answer6开头的计算行,并将其粘贴到工作表中,以覆盖A列和B列中的所有行
此方法将产生以下结果:
Question1 Question2 Question3 Question4 Question5
Answer1 Answer2 Answer3 Answer4 Answer5
Answer6 Answer7 Answer8 Answer9 Answer10
干杯。如果没有太多这样的组,用转置粘贴将是最简单的方法……不幸的是,大约有400000行,但我同意最小的行,转置它们将很容易。
Question1 Question2 Question3 Question4 Question5
Answer1 Answer2 Answer3 Answer4 Answer5
Answer6 Answer7 Answer8 Answer9 Answer10