用于数据聚合的VBA或excel公式

问题描述 投票:1回答:1

我在excel中有一张表,其中列出了公司和各种公司的人员,每家公司可能有5-7人与公司有关。该表的结构为公司名称,person1,person 2 ...在单元格中

我需要一个VBA或excel公式的方法,其中根据这个表,我希望有与每个人相关联的公司的名称,例如。 person1 - 在他名下的单元格中正在处理的所有公司的列表。

我对VBA有基本的了解,非常感谢任何方法或解决方案。

我不允许嵌入图片。所以链接源表:1https://i.stack.imgur.com/gMyy8.png

enter image description here

期望的输出:1https://i.stack.imgur.com/x8QxU.png

enter image description here

excel vba
1个回答
0
投票

如果您可以更改数据,如图中的两列,您仍然可以使用数据透视表来获取每人的公司列表。例如通过图片中的过滤(抱歉荷兰布局)。

enter image description here

否则,如果你至少知道所有人的名字,我建议在VBA中使用以下类型的循环

Dim ws as worksheet
Dim wst as worksheet
Dim i,j,k as long
Set ws = Thisworkbook.sheets("SheetNameData")
Set wst = Thisworkbook.sheets("SheetNameLists")
For i = 2 to ws.cells(rows.count,1).end(xlup).row 'Assuming you have headers, start at row 2
   For j = 2 to ws.cells(i,columns.count).end(xltoLeft).column 'Loop through all names in a companyrow
     k = application.worksheetfunction.match(ws.cells(i,j).value,wst.range("1:1"),0) 'find the right column in the output worksheet
     wst.cells(rows.count,k).end(xlup).Offset(1,0).value = ws.cells(i,1).value 'Place the companyname in the column of the right person
   Next j
Next i

对于此脚本,您需要已有第二个工作表,其中第一行包含人员的姓名

© www.soinside.com 2019 - 2024. All rights reserved.