组合两个date范围的重叠表

我想在Excel中将两个表格与date范围结合起来。

我有一些季节性定价的物业,而且我有一些有其他季节性边际的套餐,我需要把两者的date括号结合起来。

我可以在VBA中找出所有的保证金/定价的东西。 它包含在这里,因为否则在某些情况下数据看起来是相同的,即使它不是。 但是即使开始合并date,我也没有运气。

这是主表; join这两个表/范围创build的任何date都应包含在这些date中:

注册数据

我需要将这种types的区域数据与特定的属性数据合并:

基础支柱数据

而当我结合起来,它们需要看起来像这样:

组合数据

我可以轻松地将数据转储到SQL中,但是我需要公司里任何人都可以从Excel电子表格中复制的东西。

我已经尝试了各种公式计划 – 做到这一点,那么,那么这个其他的事情。 我已经尝试过使用权限查询进行交叉连接,然后试图消除我不想要的date。 没有任何作品。

我从gitgo知道它想用VBA来完成,而我尝试过的其他所有东西都是有点拖延战术的。 问题是,我似乎甚至不能把我的头围绕所需的逻辑。 我有一个以上的逻辑用例。

所有属性date需要在主表中存在(在一个范围内)。 属性可能有根本不存在于主要范围内的date。

我相信有更有效的方法,但这里是我如何使用数据和输出范围命名表。 你应该能够修改它以适应。 我想,逻辑有点复杂。 在代码下是我的testing输出与您的表匹配的屏幕抓取。

Option Explicit Sub TableMerge() Dim i As Integer Dim j As Integer Dim insert_row As Integer Dim prev_FINISH As Date Dim Table_1 As ListObject Dim Table_2 As ListObject insert_row = 2 prev_FINISH = CDate("01/01/1900") Set Table_1 = ActiveSheet.ListObjects("Table1") Set Table_2 = ActiveSheet.ListObjects("Table2") For i = 1 To Table_2.ListRows.Count For j = 1 To Table_1.ListRows.Count ' assumes the headers are in place, using range L:R for Table3 If (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("REG").Index) = Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("REG").Index)) And (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Finish").Index) > Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Start").Index)) And (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("sTART").Index) < Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Finish").Index)) Then If (prev_FINISH = CDate("01/01/1900") And (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Start").Index) <= Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Finish").Index))) Or (prev_FINISH >= Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Start").Index)) Or (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Finish").Index) >= Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Start").Index)) Then 'If (prev_FINISH = CDate("01/01/1900") And (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Start").Index) <= Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Finish").Index)) 'Or (Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Finish").Index) >= Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Finish").Index))) Then ' add new entry ActiveSheet.Range("L" & insert_row).Value = Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("REG").Index) ActiveSheet.Range("M" & insert_row).Value = Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Name").Index) ActiveSheet.Range("N" & insert_row).Value = maxoftwo(maxoftwo(Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Start").Index), Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Start").Index)), prev_FINISH) ActiveSheet.Range("O" & insert_row).Value = minoftwo(Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("Finish").Index), Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Finish").Index)) ActiveSheet.Range("P" & insert_row).Value = Table_1.DataBodyRange.Cells(j, Table_1.ListColumns("MARG").Index) ActiveSheet.Range("Q" & insert_row).Value = Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("COST").Index) ActiveSheet.Range("R" & insert_row).Formula = "=Q:Q/(1-P:P)" If ActiveSheet.Range("O" & insert_row).Value <> Table_2.DataBodyRange.Cells(i, Table_2.ListColumns("Finish").Index) Then prev_FINISH = ActiveSheet.Range("O" & insert_row).Value Else prev_FINISH = CDate("01/01/1900") j = 1 insert_row = insert_row + 1 GoTo Next_i End If insert_row = insert_row + 1 End If End If Next j prev_FINISH = CDate("01/01/1900") Next_i: Next i End Sub Function maxoftwo(date1 As Date, date2 As Date) As Date maxoftwo = date1 If date2 > date1 Then maxoftwo = date2 End Function Function minoftwo(date1 As Date, date2 As Date) As Date minoftwo = date1 If date2 < date1 Then minoftwo = date2 End Function 

在这里输入图像说明