在EXCEL 2003中将信息从1列拆分为4列

时间:2018-03-08 11:43:12

标签: excel vba excel-vba excel-2003

嘿伙计们,我有一个excel列表文件2003版本,其中包含有关雇主的信息..

我每年都会收到同一列中的信息:

here

某事日期:

    SECTOR X
NAME
KETCHUP, ASH
25/01/2017
31/02/2017
DORKMAN, RYAN
28/01/2017
30/05/2017
PEROTI, MAMA
26/01/2017
13/02/2017
28/06/2017
 SECTOR Y 
NAME
JIMENEZ, PEPE
16/01/2017
REDFIED, CHRIS
12/01/2017
JUMILLA , MANUEL
02/01/2017
12/01/2017
22/07/2017
30/07/2017
 SECTOR U 
NAME
KENEDY, LION
16/04/2017
VALENTINE, JILL
12/07/2017
KEPER, KNOR
02/03/2017
12/03/2017
22/10/2017
30/10/2017

我的问题是我在上一个例子中复制和粘贴,并且非常累人,因为我在他们各自的月份里有超过100个名字。

我像这样粘贴:

here

某事日期

SECTORS       NAME             DATE    TOTAL
SECTOR X      KETCHUP, ASH     jan/17    1
                               feb/17    1
              DORKMAN, RYAN    jan/17    1
                               may/17    1
              PEROTI, MAMA     jan/17    1
                               feb/17    1
                               jun/17    1
SECTOR Y            
             JIMENEZ, PEPE     jan/17    1
             REDFIED, CHRIS    jan/17    1
             JUMILLA , MANUEl  jan/17    2
                               juL/17    2
 SECTOR U           
             KENEDY, LION      apr/17    1
             VALENTINE, JILL   jan/17    1
             KEPER, KNOR       mar/17    2
                               oct/17    2

我试图做一个dinamic表,但我不知道4列中的信息是如何分开的,因为所有都在同一列中,我怎样才能在列中转换“扇区名称”?有什么建议或想法吗?

1 个答案:

答案 0 :(得分:1)

我建议首先将宏重新格式化为可旋转格式,然后应用数据透视表以获得所需的输出。

您希望此宏向下运行,并对每个单元格应用以下第一条规则:

  1. 如果下一个值为" Name",则将当前值存储为a Person Name
  2. 如果当前值为"名称",则不执行任何操作
  3. 如果当前值日期,则将其存储为Sector Name
  4. 如果当前值 是一个日期,那么在我们的表中添加一个新行 使用Person NameDate和当前值Sector | Name | Date
  5. 这将为每个日期添加一行,Sector和Person Name在同一行中,然后您可以将其转换为数据透视表:

    Sector X| Ketchup, Ash | 25/01/2017
    Sector X| Ketchup, Ash | 31/02/2017
    Sub MakePivotable(ByRef SourceColumn AS Range, ByRef Output As Range) Dim WorkCell As Range, OutRow As Range Dim Sector As String, Person As String Set Output = Output.Cells(1,1) 'We only want the top-left cell here Set SourceColumn = Intersect(SourceColumn, SourceColumn.Worksheet.UsedRange) 'Ignore unused rows Output.Value = "Sectors" Output.Offset(0,1).Value = "Name" Output.Offset(0,2)Value = "Date" OutRow = 1 Sector = "" Person = "" For Each WorkCell In SourceColumn.Cells IF WorkCell.Offset(1,0).Value = "Name" Then 'Is Sector Sector = WorkCell.Value ElseIf WorkCell.Value = "Name" Then 'Do Nothing ElseIf Not IsDate(WorkCell.Value) Then ' Is Name Person = WorkCell.Value ElseIf 'Is Date 'Create the row Output.Offset(OutRow,0).Value = Sector Output.Offset(OutRow,0).Value = Name Output.Offset(OutRow,0).Value = cDate(WorkCell.Value) OutRow = OutRow+1 'Move on to the next row End If Next WorkCell End Sub

    {{1}}