简体   繁体   English

如何相对于另一个在excel中转​​换列?

[英]How to transpose a column in excel relative to another?

Is there a way to do transpose a column with respect to another in excel. 有没有一种方法可以在Excel中相对于另一列进行转置。 What i am trying to do is similar to what Proc Transpose in SAS or The reshape package in R does. 我正在尝试做的事情与SAS中的Proc Transpose或R中的Reshape包类似。

A   1
A   2
B   1
B   2
B   4

I want it to transformed into this format in excel. 我希望将其转换为Excel中的这种格式。

A   1   2   
B   1   2   4

This formula will do what you want, I put "A" in D1 and "B" in D2. 该公式将满足您的要求,我在D1中输入“ A”,在D2中输入“ B”。 Put this in E1 and copy over and down: 将其放入E1并上下复制:

=IFERROR(INDEX($B:$B,IF(COLUMN(A:A)>COUNTIF($A:$A,$D1),1500000,MATCH($D1,$A:$A,0)+COLUMN(A:A)-1)),"")

Make sure to copy over and down sufficient to cover all data. 确保上下复制足以覆盖所有数据。

在此处输入图片说明

One possible solution for you is as follows. 为您提供一种可能的解决方案如下。 Appologies if it is a bit convoluted, I just woke up and the fog in my head has not cleared. 如果说有点混乱,我就醒了,脑袋里的雾还没有消除。

In D2 place this: 在D2中放置:

=IFERROR(INDEX($A$1:$A$5,MATCH(0,INDEX(COUNTIF($D$1:D1,$A$1:$A$5),0,0),0)),"")

It will generate a unique list of items from column A 它将从A列生成唯一的项目列表

Then in column E2 use the following: 然后在E2列中使用以下内容:

=IF(COUNTIF($A$1:$A$5,$D2)>=COLUMN(A:A),INDEX($B$1:$B$5,MATCH($D2,$A$1:$A$5,0)+COLUMN(A:A)-1),"")

It will pull the items in column B that correspond to column A. Copy it to the right as far as you need to go. 它将拉出B列中与A列相对应的项目。将其复制到所需的右侧。

Both those formulas can be copied down as far as you need to go. 可以根据需要将这两个公式都向下复制。

BONUS 奖金

Place the following in E1. 将以下内容放在E1中。 It is an array formula so instead of pressing enter when you are done with the formula, you press CTRL+SHFT+ENTER. 这是一个数组公式,因此在按公式完成操作时,不要按Enter,而是按CTRL + SHFT + ENTER。 This is also why these formulas are also referred to as CSE formulas sometimes. 这也是为什么这些公式有时也称为CSE公式的原因。

=MAX(COUNTIF($A$1:$A$5,D2:D3))&" Columns are required"
or
=MAX(COUNTIF(A1:A5,A1:A5))&" Columns are required"
'Thanks to Scott Craner for the second formula here

You will know you have entered it correctly when {} appear around the formula in the formula bar. 当{}出现在公式栏中的公式周围时,您将知道输入正确。 You cannot add these {} manually. 您无法手动添加这些{}。

CAVEAT 警告

The data needs to be grouped. 数据需要分组。

Proof of concept: 概念证明:

在此处输入图片说明

With data in columns A and B , try this short macro: 对于AB列中数据,请尝试以下简短宏:

Sub ReOrganizer()
    Dim i As Long, j As Long, Na As Long, K As Long
    Dim s As String, rc As Long, Nd As Long

    rc = Rows.Count
    Range("A:A").Copy Range("D:D")
    Range("D:D").RemoveDuplicates Columns:=1, Header:=xlNo
    Na = Cells(rc, "A").End(xlUp).Row
    Nd = Cells(rc, "D").End(xlUp).Row

    For i = 1 To Nd
        s = Cells(i, "D").Value
        K = 5
        For j = 1 To Na
            If s = Cells(j, "A").Value Then
                Cells(i, K).Value = Cells(j, "B").Value
                K = K + 1
            End If
        Next j
    Next i
End Sub

在此处输入图片说明

NOTE: 注意:

  • the data in column A is not required to be sorted. A列中的数据不需要排序。

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

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