简体   繁体   English

如何在Excel中的新工作表中将列转置为矩阵?

[英]How do I transpose a column to a matrix in a new sheet in Excel?

I am not much of an Excel user, but one client is insisting I use it. 我不是一个Excel用户,但是一个客户坚持要使用它。

I have data in 1728 cells in a column. 我在一列的1728个单元格中有数据。 I want to convert it to a 64x27 matrix in a new sheet. 我想将其转换为新工作表中的64x27矩阵。 I found the OFFSET function and tried 我找到了OFFSET函数并尝试

=OFFSET(ThreePeople!A2:A1729,64,27)

but this gave an error (!VALUE) and asking for "help on this error" was not helpful. 但这给出了一个错误(!VALUE),并且请求“对此错误的帮助”没有帮助。

What am I doing wrong? 我究竟做错了什么?

EDIT: Apparently OFFSET is the wrong function. 编辑:显然OFFSET是错误的功能。 Sorry. 抱歉。

I want a matrix with 64 rows and 27 columns. 我想要一个64行27列的矩阵。 Cell A2 in the column would go to cell A1 in the new sheet, then: 列中的单元格A2将转到新工作表中的单元格A1,然后:

A3 to A2 .....A28 to A27, A65 to A64, A66 to B2 ..... and so on. A3至A2 ..... A28至A27,A65至A64,A66至B2 .....依此类推。

I did this in SAS with this code: 我在SAS中使用以下代码进行了此操作:

 horiz = mod(location-1, 27);
 vert = int(location/27) + 1;

where "location" would be the row number of the original data and horiz and vert would be the places where I want the new data. 其中“位置”将是原始数据的行号,horiz和vert将是我想要新数据的地方。

Put either formula below in cell A1 : 将以下任一公式放在单元格A1

To get 27x64 use this formula: 要获得27x64,请使用以下公式:

=OFFSET(ThreePeople!$A2,COLUMNS(ThreePeople!$A2:A2)-1+(ROWS($1:1)-1)*64,0)

Drag it to cell AA1 and then drag that range 27 rows down. 将其拖动到单元格AA1 ,然后将该范围向下拖动27行。 This fill from left to right / top to bottum. 此填充从左到右/从上到下。

To get 64x27 use this formula: 要获得64x27,请使用以下公式:

=INDEX(ThreePeople!$A$2:$A$1729,ROW(A1)+(64*(COLUMNS($A$1:A$1)-1)))

Drag it down to cell A64 then drag the range to column 27 (AA). 将其向下拖动到单元格A64然后将范围拖动到列27(AA)。 This fills from top to bottom / left to right. 这从上到下/从左到右填充。

you could create a macro that would do it for you 您可以创建一个宏来为您做

Sub SplitColumn()

Dim rng As Range
Dim InputRng As Range
Dim OutRng As Range
Dim xRow As Integer
Dim xCol As Integer
Dim xArr As Variant
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
xRow = Application.InputBox("Rows :", xTitleId)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set InputRng = InputRng.Columns(1)
xCol = InputRng.Cells.Count / xRow
ReDim xArr(1 To xRow, 1 To xCol + 1)
For i = 0 To InputRng.Cells.Count - 1
    xValue = InputRng.Cells(i + 1)
    iRow = i Mod xRow
    iCol = VBA.Int(i / xRow)
    xArr(iRow + 1, iCol + 1) = xValue
Next
OutRng.Resize(UBound(xArr, 1), UBound(xArr, 2)).Value = xArr
End Sub

After this you run the macro, select the cells you want to transpose, type in how many rows and select the output cell 之后,运行宏,选择要转置的单元格,键入多少行,然后选择输出单元格

我会使用INDEX()

=INDEX(ThreePeople!$A:$A,ROWS($1:1)+1+(COLUMNS($A:A)-1)*64)

=INDEX(newsheet!$A:$A,ROW(A1)*64-64+COLUMN(A1)) =索引(newsheet!$ A:$ A,ROW(A1)* 64-64 + COLUMN(A1))
fill formula 64 right and 27 down (a1=1, a2=2 etc) 填写公式64右27向下(a1 = 1,a2 = 2等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM