将单列文本分类为多列

时间:2013-10-24 00:55:15

标签: excel excel-vba vba

我正在尝试创建一个宏,该宏将根据项类型将一列中的数据分类为多个列。我试图分类的数据是合同列表,其中包含合同中项目的元数据。

原始数据如下所示:

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的代码由Oscar提供。 #
这是我的代码,我完全赞同Oscar的教程,让我朝着正确的方向前进。

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

1 个答案:

答案 0 :(得分:0)

您可以考虑使用可提供类似输出的数据透视表。

enter image description here

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

enter image description here

enter image description here