简体   繁体   English

将列值转换为行值

[英]Transpose the column values into row values

I would like to use excel 2010 VBA to realize a function to transpose the value from column to row, and if there are more than 10 values in the same row, the 11th value return to the second row. 我想使用excel 2010 VBA实现一个将值从列转移到行的函数,如果同一行中有10个以上的值,则第11个值将返回第二行。

For example: 例如:

========= =========

ColA  ColB
1111  AAA
1111  BBB
1111  CCC
1111  DDD
1111  EEE
1111  FFF
1111  GGG
1111  HHH
1111  III
1111  JJJ
1111  KKK
1111  LLL
2222  MMM
2222  OOO
2222  PPP

Desired: 期望:

ColA   Val1   Val2   Val3    Val4   Val5   Val6   Val7    Val8    Val9    Val10 
1111   AAA    BBB    CCC    DDD     EEE    FFF    GGG     HHHH    III     JJJ
1111   KKK    LLL  
2222   MMM    OOO    PPP 

========= =========

I've tried to first group the value into one field with delimiter "," and then I use the excel function to separate the data into different columns. 我试图先用定界符“,”将值分组到一个字段中,然后使用excel函数将数据分为不同的列。 here is the code to group the values but i dont know how to tell excel to go the second row if there are more than 10 values. 这是将值分组的代码,但如果有10个以上的值,我不知道如何告诉excel去第二行。

here is the code to group the values: 这是将值分组的代码:

 Sub combineValues()
    Dim dic As Dictionary
     Dim key, val, i, p, k
    Set dic = New Dictionary
    For i = 1 To Worksheets(1).Range("A65536").End(xlUp).Row
        key = Worksheets(1).Cells(i, 1).Value
        val = Worksheets(1).Cells(i, 2).Value
        If dic.Exists(key) Then
            dic.Item(key) = dic.Item(key) & ", " & val
        Else
            dic.Add key, val
        End If
    Next
    p = 1
    For Each k In dic.Keys
        Worksheets(2).Cells(p, 1) = k
        Worksheets(2).Cells(p, 2) = dic.Item(k)
        p = p + 1
    Next
End Sub

with the code, I can group the value into one row, like this: 使用代码,我可以将值分组为一行,如下所示:

ColA  ColB
1111  AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,III,JJJ,KKK,LLL
2222  MMM,OOO,PPP

and then I use excel function to separate all these value into different fields, mainly like this: 然后我使用excel函数将所有这些值分成不同的字段,主要是这样的:

ColA   Val1   Val2   Val3   Val4   Val5   Val6   Val7   Val8   Val9    Val10 Val11 Val12 
1111   AAA    BBB    CCC    DDD     EEE    FFF    GGG    HHH    III     JJJ  KKK    LLL  
2222   MMM    OOO    PPP 

but the problem is I don't want more than 10 values appear in the same row, I want to tell if there are more than 10 values, it return to the second row for the rest value. 但是问题是我不想在同一行中出现超过10个值,我想知道是否有10个以上的值,它返回到第二行以显示其余值。

Okay so here's my second try on this: 好的,这是我的第二次尝试:

Sub test2()
    Dim search As Long
    Dim j As Long
    Dim l As Long
    Dim cCount As Long
    Dim aCount As Long

    aCount = 1

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        l = 2
        j = i
        cCount = 0

        search = Cells(i, 1).Value
        Worksheets("test").Cells(aCount, 1).Value = Cells(i, 1).Value
        While search = Cells(j, 1).Value
            If l = 12 Then
                aCount = aCount + 1
                Worksheets("test").Cells(aCount, 1).Value = Cells(i, 1).Value
                l = 2
            Else
             Worksheets("test").Cells(aCount, l).Value = Cells(j, 2).Value
                j = j + 1
                l = l + 1
                cCount = cCount + 1
            End If
        Wend
        aCount = aCount + 1
        i = i + cCount - 1
    Next i
End Sub

This time you enter this: 这次您输入以下内容:

在此处输入图片说明

and get this: 并得到这个:

在此处输入图片说明

This time it checks for the value of ColA and as long as the value of ColA is the same it will put the value of ColB in the next column to the right with a break every 10th column. 这次它将检查ColA的值,只要ColA的值相同,它将把ColB的值放在右边的下一列中,每隔10列就有一个间隔。

best regards Amnney 最好的问候安妮

Maybe my english isn't good enough to understand the issus but isn't that the result you desired? 也许我的英语不够好,无法理解问题,但这不是您想要的结果吗?

Before: 之前:

在此处输入图片说明

After: 后:

在此处输入图片说明

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

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