在Excel中自动合并两个不规则的时间序列

时间:2014-03-30 03:23:06

标签: excel vba

以下是这种情况:

  • 我在两个源数据系列中提供了数据。
  • 两个源数据系列都具有相同的列,其中一列包含日期。
  • 每行都是唯一的,但可能有多行具有相同的日期值。
  • 我需要创建一个合并数据系列,其中包含按日期排序的源数据系列中的所有行。
  • 源系列需要保留在电子表格中自己的列中,因此我们不能简单地将这两个系列复制并粘贴到每个系列中并进行排序。
  • 我们需要一些解决方案,一旦输入源系列的数据,就会自动生成合并的数据系列。

这里的closest questions I've found似乎都依赖于R,但有没有办法在Excel中执行此操作?

编辑:这里有一些简化的样本数据(实际数据包含更多列)和所需的结果:

数据来源1:

Type    Date       Num  Name     Split        Amount
Deposit 01/01/2014 wire John Doe Town Bank     $1234.56
Deposit 01/06/2014 wire Jane Doe Village Bank  $3434.65
Check   02/24/2014 clr  Jim Doe  City Bank    -$4000.00

数据来源2:

Type    Date       Num  Name     Split        Amount
Deposit 12/11/2013 45   John Doe Town Bank      -$25.56
Deposit 12/25/2013 wire Jane Doe Village Bank   $334.65
Deposit 01/11/2014 45   John Doe Town Bank      -$25.56
Deposit 01/14/2014 wire Jane Doe Village Bank   $108.00
Deposit 01/27/2014 wire Jane Doe Village Bank   $314.15
Deposit 02/11/2014 45   John Doe Town Bank      -$25.56
Deposit 02/13/2014 wire Jane Doe Village Bank     $2.15

期望的结果:

Type    Date       Num  Name     Split        Amount
Deposit 12/11/2013 45   John Doe Town Bank      -$25.56
Deposit 12/25/2013 wire Jane Doe Village Bank   $334.65
Deposit 01/01/2014 wire John Doe Town Bank     $1234.56
Deposit 01/06/2014 wire Jane Doe Village Bank  $3434.65
Deposit 01/11/2014 45   John Doe Town Bank      -$25.56
Deposit 01/14/2014 wire Jane Doe Village Bank   $108.00
Deposit 01/27/2014 wire Jane Doe Village Bank   $314.15
Deposit 02/11/2014 45   John Doe Town Bank      -$25.56
Deposit 02/13/2014 wire Jane Doe Village Bank     $2.15
Check   02/24/2014 clr  Jim Doe  City Bank    -$4000.00

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

Sub Tester()

    Dim rng1 As Range, rng2 As Range, cr As Range

    Set rng1 = Worksheets("Sheet1").Range("A1").CurrentRegion
    Set rng1 = rng1.Offset(1, 0).Resize(rng1.Rows.Count - 1) 'omit headers

    Set rng2 = Worksheets("Sheet2").Range("A1").CurrentRegion
    Set rng2 = rng2.Offset(1, 0).Resize(rng2.Rows.Count - 1) 'omit headers

    With Worksheets("Summary")

        Set cr = .Range("A1").CurrentRegion

        cr.Offset(1, 0).ClearContents 'clear existing data
        rng1.Copy .Range("A2")
        rng2.Copy .Range("A2").Offset(rng1.Rows.Count)

        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=cr.Columns(2), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
            .SetRange cr
            .Header = xlYes
            .MatchCase = False
            .Apply
        End With

    End With
End Sub