简体   繁体   English

Excel VBA 表操作:基于第一列转置列

[英]Excel VBA table manipulation: transpose column based on first column

I want to organize a table generated by importing from multiple text files.我想组织一个通过从多个文本文件导入生成的表格。 Importing the multiple text files into excel creates a table similar to the dataset below.将多个文本文件导入 excel 会创建一个类似于以下数据集的表格。 It has the text file name in column A and the values are in Column B.它在 A 列中具有文本文件名,而值在 B 列中。

  1. Number of groups is around 400团体人数约400人
  2. Number of "IDs" is always 160 “ID”的数量始终为 160
  3. Using a formula would be great, but I figured that wouldn't be possible使用公式会很棒,但我认为那是不可能的

Here is my data set.这是我的数据集。 It has only two columns as described but needs to be transposed so that there is one row per distinct column 1 entry, which will then have multiple columns:它只有描述的两列,但需要转置,以便每个不同的列 1 条目有一行,然后将有多列:

|Group|Value|
|-----|-----|
|A.txt| 1a  |
|A.txt| 2a  |
|A.txt| 3a  |
|B.txt| 1b  |
|B.txt| 2b  |
|B.txt| 3b  |
|C.txt| 1c  |
|C.txt| 2c  |
|C.txt| 3c  |

Here's the desired result!这是想要的结果!

|Group|ID1|ID2|ID3|
|:----|:-:|:-:|--:|
|A.txt|1a |2a |3a |
|B.txt|1b |2b |3b |
|C.txt|1c |2c |3c |

The number of IDs are the same for each entry.每个条目的 ID 数量相同。

I have used VBA in excel many years ago, but I haven't since 2015. Needing some help.很多年前,我曾在 excel 中使用过 VBA,但自 2015 年以来我就没有使用过。需要一些帮助。

I know you asked for an Excel/VBA solution, but I will give you a very easy Google Sheets one.我知道您要的是 Excel/VBA 解决方案,但我会给您一个非常简单的 Google 表格解决方案。 You can easily open you Excel over at goolge sheets and work there.您可以轻松地在 Google 表格中打开 Excel 并在那里工作。 You may even get the Idea and port it to Excel then.你甚至可以得到这个想法,然后将它移植到 Excel 中。 Just put in the same places and write the formulas at the assigned cells, or adjust the formulas...只需放在相同的位置并在指定的单元格中写入公式,或调整公式...

在此处输入图片说明

I paste the code here as well:我也在这里粘贴代码:

D2: =UNIQUE(A2:A)
E1: = ARRAYFORMULA("ID" & TEXT(SEQUENCE(1,COUNTA(E2:2),1,1),"00"))
E2: =TRANSPOSE( filter($B$2:$B,$A$2:$A=$D2))
e3... drag down from E2

Here is the solution I came up.这是我提出的解决方案。 It took me a year to have 3 hours to work on this project.我花了一年时间才有 3 个小时来完成这个项目。 I'll likely at some point in the near future extend this to sort and use VLOOKUP(), but this exercise was the starting point I needed to remember how to use VBA.我可能会在不久的将来将其扩展到排序和使用 VLOOKUP(),但这个练习是我需要记住如何使用 VBA 的起点。

This is a first draft requiring to create a button, but will have VBA load a each of the txt files from a folder and create the initial set than run the macro below against that.这是需要创建按钮的初稿,但将 VBA 从文件夹中加载每个 txt 文件并创建初始集,然后针对该按钮运行下面的宏。

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim group As Integer
Dim str1 As String
Private Sub CommandButton1_Click()
    str1 = ""
    group = 0
    j = 0
    k = 1
    For i = 1 To 10
        If str1 = Cells(i, 1).Value Then
            k = k + 1
            Cells(group, 4 + k).Value = Cells(i, 2).Value
        Else
            str1 = Cells(i, 1).Value
            If group = 0 Then
                Cells(1, 4).Value = Cells(i, 1).Value
                j = j + 1
            Else
                Cells(1, 4 + group).Value = CStr(group)
                Cells(group + 1, 4).Value = str1
                Cells(group + 1, 4 + j).Value = Cells(i, 2).Value
            End If
            group = group + 1
            k = 1
        End If
    Next i
End Sub

Sincerely真挚地

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

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