首页 > 解决方案 > 如何将人员数据移动到与 VBA 链接的工作表

问题描述

我有一张包含一些虚构个人数据的表格:电话号码、地址等。我有大约 200 个不同的人,所以我制作了一个 vba,它可以根据人名自动创建表格。而且我对 vba 很陌生,并且知道如何使用例如复制数据..

Sheets("Ark1").Range("A1:O10").Copy Destination:=Sheets("Ark2").Range("A1")

但这仅适用于该特定的工作表目的地。我如何根据工作表(“Ark1”)中的他们的名字。从“Ark1”中移动属于特定人员的数据。如链接图像所示,B 列是一个人的数据,C 是另一个等。搜索条件应该是他们的名字,因为这是我定义不同工作表的内容,他们的名字在数据文件的“row2”中“方舟一号”。

更新的数据集

标签: excelvba

解决方案


我很难理解你需要做什么。您是根据 row2 中的名称手动创建工作表,还是仍然需要制作它们?现在您想将 Sheets(1) 中的所有信息复制到 ark1 工作表中吗?还有什么是表(1)?亨利克·詹森斯表?您的屏幕截图暗示您需要 ark 表中的信息,但您的代码暗示您复制到 ark1 表。

我最好的猜测,根据需要进行调整:

Sub test()

'See how many columns you have on your sheet (I'm assuming your Ark1 sheet has the input)
    For i = 2 To ThisWorkbook.Sheets("Ark1").Cells(2, Columns.Count).End(xlToLeft).Column

        'This creates new sheets with the names from the second row (was this done already?)
            ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = _
            ThisWorkbook.Sheets("Ark1").Cells(2, i)

        'Pick one of the next three, depending on what you need
            '1.) This is where I'm confused but you should be able to adjust this as needed.
                ThisWorkbook.Sheets(ThisWorkbook.Sheets("Ark1").Cells(2, i).Value).Range("A1:O10").Value = _
                ThisWorkbook.Sheets("Sheet1").Range("A1:O10").Value 'I dont see a sheet1 in your screenshot but your code has it This is the source tho

            '2.) Maybe you mean this tho?
                ThisWorkbook.Sheets(ThisWorkbook.Sheets("Ark1").Cells(2, i).Value).Columns(1).Value = _
                ThisWorkbook.Sheets("Ark1").Columns(i).Value

            '3.) if you need to copy
                ThisWorkbook.Sheets("Ark1").Columns(i).Copy _
                Destination:=ThisWorkbook.Sheets(ThisWorkbook.Sheets("Ark1").Cells(2, i).Value).Columns(1)
    Next

End Sub

这也会破坏第 2 行中的相同名称和空单元格,根据需要创建处理程序。应该以其他方式工作吗?不确定 .value 是否需要。


推荐阅读