基于单个范围值创建和保存多个工作表

时间:2018-01-29 23:08:25

标签: excel vba excel-vba

我正在使用Excel根据客户的名称将以下表格拆分成多张表格:

Please click to see image

Client  Plan    Toal
A       Master   189,50 
A       Master   289,50 
B       Master   239,50 
B       Master   189,50 
B       Master   339,50 
C       Master   239,50 
D       Master   189,50 
E      Master    239,50 

因此,在上面的示例中,我需要为每个客户端自动创建5个工作表。我知道如何使用过滤条件来写这个(criteria1 =" A",criteria2 =" B"),问题是:上面的文件可能包含' n'不同的客户,所以我不能使用固定的标准。我想我需要为每个结构或同时但不知道如何做到这一点。有谁可以帮我解决这个问题?

全部谢谢!

1 个答案:

答案 0 :(得分:0)

我能想到的最简单的方法是生成A列唯一成员的列表。您没有指定您是否在A列中始终拥有相同数量的项目,但是我假设你想从A2开始并继续直到你到达一个空白单元格。然后你可以循环遍历它们:

'This stores your unique values
Dim Unique As New Collection
'This variable represents each cell in column A
Dim cll As Range

'If a reference already exists, it will throw an error and jump to the next cell
On Error Resume Next

'Starting at A2 since you don't want to include the heading
For Each cll In Range("A2:A1048576")
    'Exits if there's no name in the cell
    If cll.value = "" Then Exit For
    'Otherwise, add the cell value to your collection
    Unique.Add cll.Value
Next cll

'Turn error handling off
On Error GoTo 0
'Loop through your unique names
For Each Item In Unique
    '<Your code to create a worksheet goes here. "Item" contains each name
    'in the Unique collection>
Next Item