2013-10-24 147 views
3

我试图创建一个宏,它将根据项目类型将一列中的数据分为多列。我试图分类的数据是合同中有关项目元数据的合同列表。将单列文本分为多列

的原始数据是这样的:

 
Contract No Contract Name   Item Type Item Description 
111111  Chocolate Supplies  POTS  5" 
111111  Chocolate Supplies  POTS  10" 
111111  Chocolate Supplies  POTS  15" 
111111  Chocolate Supplies  PANS  5" 
111111  Chocolate Supplies  PANS  10" 
111111  Chocolate Supplies  PANS  15" 
111111  Chocolate Supplies  KNIVES  Paring knife 
111111  Chocolate Supplies  SILVERWARE Salad fork 
111111  Chocolate Supplies  SILVERWARE Dinner fork 
111111  Chocolate Supplies  SILVERWARE Dessert fork 
111111  Chocolate Supplies  SILVERWARE Dessert spoon 
111111  Chocolate Supplies  SILVERWARE Soup spoon 
22222  Soups and Salads Order  POTS  10" 
22222  Soups and Salads Order  POTS  15" 
22222  Soups and Salads Order  PANS  15" 
22222  Soups and Salads Order  KNIVES  Butter knife 
22222  Soups and Salads Order  KNIVES  Bread knife 
22222  Soups and Salads Order  KNIVES  Paring knife 
22222  Soups and Salads Order  SILVERWARE Soup spoon 

的最终数据需要看起来像这样(编辑成包括图像): Final Format of Report

 
Contract Contract Name   POTS PANS KNIVES   SILVERWARE 
111111  Chocolate Supplies  5"  5"  Paring knife Salad fork 
            10"  10"      Dinner fork       
            15"  15"      Dessert fork 
                    Dessert spoon 
                    Soup spoon 
22222  Soups and Salads Order 10"  15"  Butter knife Soup spoon 
            15"    Bread knife 
                Paring knife  

#我一直如此远#
我目前使用的原油解决方案是:
- 运行查询
- 将数据粘贴到Excel
- 创建枢轴
- 使用一系列计数,根据需要
偏移和间接公式来重新组织数据 - 由于上述过程留下合同的每个部分之间的空行,我将数据复制粘贴到新的工作表中,放置一个自动过滤器并删除空白行
...和瞧,这是最终报告。

#可能的解决方案VBA#
我发现this tutorial这似乎做正是我想要的,除了我需要宏开始一个新的部分问题时,合同没有。变化。我不知道如何获得下面的VBA代码来检查合同号。

我很乐意提供任何帮助。提前致谢!

#代码从tutorial on get-digital-help [dot] com由奥斯卡。 #
这是不是我的代码,我完全称赞奥斯卡的教程让我朝着正确的方向前进。

Sub Categorizedatatocolumns() 
Dim rng As Range 
Dim dest As Range 
Dim vrb As Boolean 
Dim i As Integer 
Set rng = Sheets("Sheet1").Range("A4") 
vrb = False 
Do While rng <> "" 
Set dest = Sheets("Sheet1").Range("A20") 
Do While dest <> "" 
If rng.Value = dest.Value Then 
vrb = True 
End If 
Set dest = dest.Offset(0, 1) 
Loop 
If vrb = False Then 
dest.Value = rng.Value 
dest.Font.bold = True 
End If 
vrb = False 
Set rng = rng.Offset(1, 0) 
Loop 
Set rng = Sheets("Sheet1").Range("A4") 
Do While rng <> "" 
Set dest = Sheets("Sheet1").Range("A20") 
Do While dest <> "" 
If rng.Value = dest.Value Then 
i = 0 
Do While dest <> "" 
Set dest = dest.Offset(1, 0) 
i = i + 1 
Loop 
Set rng = rng.Offset(0, 1) 
dest.Value = rng.Value 
Set rng = rng.Offset(0, -1) 
Set dest = dest.Offset(-i, 0) 
End If 
Set dest = dest.Offset(0, 1) 
Loop 
Set rng = rng.Offset(1, 0) 
Loop 
End Sub 

回答

0

您可以考虑使用数据透视表,它会给出类似的输出。

enter image description here

关闭小计和显示数据以表格形式的所有字段。

enter image description here

enter image description here

+0

桑托斯,感谢您回应,但这并不让我的“项目类型”跨列下去。我添加了一个新的屏幕截图来显示最终报告格式的外观。 – Laurie