1. 'ClassPlan
  2.  
  3. Public Org As String
  4. Public Des As String
  5. Public FlyNo As String
  6. Public StartDate As Variant
  7. Public TextStartTime As Variant
  8. Public TextEndTime As Variant
  9. Public StartTime As Variant
  10. Public EndTime As Variant
  11. Public EndDate As Variant
  12. Public BackDate As Variant
  13.  
  14. 'mod_GetPlan
  15. Public Sub GetPlan()
  16. If Now() > #6/5/2018# Then Exit Sub
  17. Dim sht As Worksheet
  18. Dim osht As Worksheet
  19. Set osht = ThisWorkbook.Worksheets("TOTAL")
  20. Set sht = ThisWorkbook.Worksheets("Collocation-0")
  21. Dim Origin, Connecting, Destination, TripDate, Stay
  22. With sht
  23. Origin = .Range("D3").Text
  24. Connecting = .Range("F3").Text
  25. Destination = .Range("H3").Text
  26. TripDate = CDate(.Range("J3").Value)
  27. Stay = CLng(.Range("K3").Value)
  28.  
  29. .UsedRange.Offset(15).ClearContents
  30. End With
  31.  
  32. Dim dPlan As Object
  33. Dim dUsed As Object
  34. Dim dBackDate As Object
  35.  
  36. Set dPlan = CreateObject("Scripting.Dictionary")
  37. Set dUsed = CreateObject("Scripting.Dictionary")
  38.  
  39. '记录所有航班信息
  40. Dim Plan As ClassPlan
  41. With osht
  42. EndRow = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row
  43. EndCol = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByColumns, xlPrevious).Column
  44. PlanCount = 0
  45. Set Rng = .Range(.Cells(1, 1), .Cells(EndRow, EndCol))
  46. Arr = Rng.Value
  47. DateIndex = 0
  48. For j = LBound(Arr, 2) + 8 To UBound(Arr, 2)
  49. '获取初始日期
  50. If Arr(2, j) <> "" Then
  51. StartDate = DateAdd("d", DateIndex, CDate(Format(Arr(2, j), "yyyy/mm/dd")))
  52. End If
  53. '获取航班日期
  54. FlyDate = DateAdd("d", DateIndex, StartDate)
  55. DateIndex = DateIndex + 1
  56.  
  57. '逐行检查
  58. For i = LBound(Arr) + 5 To UBound(Arr)
  59. If Arr(i, j) = "Y" Then
  60. PlanCount = PlanCount + 1
  61. Set Plan = New ClassPlan
  62. With Plan
  63. .FlyNo = Arr(i, 3)
  64. .Org = Arr(i, 5)
  65. .Des = Arr(i, 6)
  66. .StartDate = FlyDate
  67. .TextStartTime = Replace(Arr(i, 7), " ", "")
  68. .StartTime = CDate(FlyDate + Arr(i, 7))
  69. If InStr(1, Arr(i, 8), "+1") > 0 Then
  70. et = CDate(Replace(Arr(i, 8), "+1", ""))
  71. .EndTime = CDate(DateAdd("d", 1, FlyDate) + et)
  72. .TextEndTime = Replace(Arr(i, 8), "+1", "")
  73. ElseIf InStr(1, Arr(i, 8), "-1") > 0 Then
  74. et = CDate(Replace(Arr(i, 8), "-1", ""))
  75. .EndTime = CDate(DateAdd("d", -1, FlyDate) + et)
  76. .TextEndTime = Replace(Arr(i, 8), "-1", "")
  77. Else
  78. .EndTime = CDate(FlyDate + CDate(Arr(i, 8)))
  79. .TextEndTime = Arr(i, 8)
  80. End If
  81.  
  82. .EndDate = CDate(Format(.EndTime, "yyyy/mm/dd"))
  83. .BackDate = Format(DateAdd("D", 0, .EndDate), "yyyy/mm/dd")
  84.  
  85. 'If .FlyNo = "S73211" Then Debug.Print "结束时间:"; .EndTime; "返回日期 :"; .BackDate
  86. 'Debug.Print .StartTime; " 抵达日期和时间 "; .EndTime
  87. End With
  88. Set dPlan(CStr(PlanCount)) = Plan
  89. End If
  90. Next i
  91. Next j
  92. End With
  93.  
  94. ' 开始寻找符合条件的航班
  95. '第一层循环 检查出发日期、出发地、中转地是否符合条件
  96. Dim OneGo, GoBefore
  97. Dim OneCnn, GoAfter
  98. Dim OneBack, BackBefore
  99. Dim OneAfter, BackAfter
  100. Dim Index As Long
  101. Dim HeadRow As Long
  102. HeadRow = 15
  103. For Each OneGo In dPlan.keys
  104. If dUsed.exists(OneGo) = False Then
  105. Set GoBefore = dPlan(OneGo)
  106. '若出发日期符合条件
  107. If Abs(DateDiff("d", GoBefore.StartDate, TripDate)) <= 3 Then
  108. '若出发地和中转地符合条件
  109. If GoBefore.Org = Origin And GoBefore.Des = Connecting Then
  110. 'Debug.Print GoBefore.FlyNo
  111. dUsed(OneGo) = ""
  112. '第二层循环 中转地、目的地、检查出发时间是否符合条件
  113. For Each OneCnn In dPlan.keys
  114. If dUsed.exists(OneCnn) = False Then
  115. Set GoAfter = dPlan(OneCnn)
  116. '若中转地和目的地符合条件
  117. If GoAfter.Org = Connecting And GoAfter.Des = Destination Then
  118. '若中转起飞时间符合条件
  119. If DateDiff("h", GoBefore.EndTime, GoAfter.StartTime) > 2 And DateDiff("h", GoBefore.EndTime, GoAfter.StartTime) < 48 Then
  120. dUsed(OneCnn) = ""
  121. 'Debug.Print GoBefore.FlyNo; " "; GoBefore.StartDate; ">>>>"; GoAfter.FlyNo; " "; GoAfter.BackDate
  122.  
  123. Set dBackDate = CreateObject("Scripting.Dictionary")
  124. '保留符合返程条件的出发日期
  125. For off = -3 To 3
  126. bd = Format(DateAdd("d", Stay + off, CDate(GoAfter.BackDate)), "yyyy/mm/dd")
  127. 'Debug.Print "回程日期 "; bd
  128. dBackDate(bd) = ""
  129. Next off
  130.  
  131. '第三层循环返程
  132. For Each OneBack In dPlan.keys
  133. If dUsed.exists(OneBack) = False Then
  134. Set BackBefore = dPlan(OneBack)
  135. '回程日期
  136. bd = Format(BackBefore.StartDate, "yyyy/mm/dd")
  137. '若回程日期符合预设范围
  138. If dBackDate.exists(bd) Then
  139. '如果出发地与中转地相符,记下航班信息
  140. If BackBefore.Org = Destination And BackBefore.Des = Connecting Then
  141. 'Debug.Print "回程航班:"; BackBefore.FlyNo; " "; BackBefore.StartDate
  142. dUsed(OneBack) = ""
  143. '第四层循环 返程中转
  144. For Each OneAfter In dPlan.keys
  145. Set BackAfter = dPlan(OneAfter)
  146. If dUsed.exists(OneAfter) = False Then
  147. '若回程中转出发地和目的地符合条件
  148. If BackAfter.Org = Connecting And BackAfter.Des = Origin Then
  149. '若中转时间符合要求
  150. If DateDiff("h", BackBefore.EndTime, BackAfter.StartTime) > 2 And DateDiff("h", BackBefore.EndTime, BackAfter.StartTime) < 48 Then
  151.  
  152. dUsed(OneAfter) = ""
  153. Index = Index + 1
  154. With sht
  155. Debug.Print "往返完全符合条件的线路" & Index
  156. .Cells(Index + HeadRow, "C").Value = Index
  157. 'GO
  158. .Cells(Index + HeadRow, "D").Value = GoBefore.FlyNo
  159. .Cells(Index + HeadRow, "E").Value = GoBefore.StartDate
  160. .Cells(Index + HeadRow, "F").Value = GoBefore.TextStartTime
  161. .Cells(Index + HeadRow, "G").Value = GoBefore.TextEndTime
  162.  
  163. .Cells(Index + HeadRow, "H").Value = GoAfter.FlyNo
  164. .Cells(Index + HeadRow, "I").Value = GoAfter.StartDate
  165. .Cells(Index + HeadRow, "J").Value = GoAfter.TextStartTime
  166. .Cells(Index + HeadRow, "K").Value = GoAfter.TextEndTime
  167. 'Back
  168. .Cells(Index + HeadRow, "L").Value = BackBefore.FlyNo
  169. .Cells(Index + HeadRow, "M").Value = BackBefore.StartDate
  170. .Cells(Index + HeadRow, "N").Value = BackBefore.TextStartTime
  171. .Cells(Index + HeadRow, "O").Value = BackBefore.TextEndTime
  172.  
  173. .Cells(Index + HeadRow, "P").Value = BackAfter.FlyNo
  174. .Cells(Index + HeadRow, "Q").Value = BackAfter.StartDate
  175. .Cells(Index + HeadRow, "R").Value = BackAfter.TextStartTime
  176. .Cells(Index + HeadRow, "S").Value = BackAfter.TextEndTime
  177.  
  178. End With
  179. End If
  180. End If
  181. End If
  182. Next OneAfter
  183. End If
  184. End If
  185. End If
  186.  
  187. Next OneBack
  188.  
  189. End If
  190. End If
  191. End If
  192. Next OneCnn
  193. End If
  194. End If
  195. End If
  196. Next OneGo
  197.  
  198. Set dUsed = Nothing
  199. Set dPlan = Nothing
  200. Set sht = Nothing
  201. Set osht = Nothing
  202. Set dBackDate = Nothing
  203.  
  204. End Sub

  

20171205xlVBA往返航班组合的更多相关文章

  1. 移动IP 它最初设想每个人都在编写应用层(7)API而不是传输层(4)API 对于QUIC,连接的标识符不是“套接字”(源/目标端口/地址协议组合)的传统概念,而是分配给连接的64位标识符

    小结: 1. 因为您对OSI模型的教育中缺少的一点是,它最初设想每个人都在编写应用层(7)API而不是传输层(4)API.应该有像应用程序服务元素之类的 东西,它们可以以标准方式处理文件传输和消息传递 ...

  2. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  3. JS继承之借用构造函数继承和组合继承

    根据少一点套路,多一点真诚这个原则,继续学习. 借用构造函数继承 在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术( ...

  4. ComponentPattern (组合模式)

    import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...

  5. 安卓自定义组合控件--toolbar

    最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式. 使用的工具为android stud ...

  6. UML类图(下):关联、聚合、组合、依赖

    前言 上一篇文章UML类图(上):类.继承.实现,讲了UML类图中类.继承.实现三种关系及其在UML类图中的画法,本文将接着上文的内容,继续讲讲对象之间的其他几种关系,主要就是关联.聚合.组合.依赖, ...

  7. 面向组合子设计Coder

    面向组合子 面向组合子(Combanitor-Oriented),是最近帮我打开新世界大门的一种pattern.缘起haskell,又见monad与ParseC,终于ajoo前辈的几篇文章. 自去年9 ...

  8. Atitit 动态按钮图片背景颜色与文字组合解决方案

    Atitit 动态按钮图片背景颜色与文字组合解决方案 转换背景颜色,setFont("cywe_img", fontScale, 50, 5) 设置文字大小与坐标 文字分拆,使用字 ...

  9. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

随机推荐

  1. Git冲突与解决方法【转】

    本文转载自:https://www.cnblogs.com/gavincoder/p/9071959.html Git冲突与解决方法 1.git冲突的场景 情景一:多个分支代码合并到一个分支时: 情景 ...

  2. Django 创建项目笔记

    基本命令 mkdir mysite # 创建项目目录,常取名mysite cd mysite virtualenv env # env\Scripts\activate.bat # Win pip i ...

  3. 大数乘法|2012年蓝桥杯B组题解析第六题-fishers

    (9')大数乘法 对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的 ...

  4. switch反汇编(C语言)

    在分支较多的时候,switch的效率比if高,在反汇编中我们即可看到效率高的原因 0x01分支结构不超过3个 #include <stdio.h> void main() { int x ...

  5. 【无法使用yum安装软件】使用yum命令安装软件提示No package numactl.x86_64 available.

    在安装mysql时需要安装numactl.x86_64 使用yum -y install numactl.x86_64时报错 [root@sdp6 mysql]# yum -y install num ...

  6. hihoCoder week7 完全背包

    完全背包 题目链接 https://hihocoder.com/contest/hiho7/problem/1 #include <bits/stdc++.h> using namespa ...

  7. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  8. ssh中的 Connection closed by ***

    另一台电脑的 mac/windows10/win7 都可以连接,就这台电脑不可以,但是能 ping 通, ssh 时总是 Connection reset by xxx 或 Connection cl ...

  9. HDU 5242 Game(贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=5242 题意: 给出一棵树,每个节点都有一个权值,每次可以获得从根结点(1)到叶子节点上的所有权值和,每个节点只能 ...

  10. HDU 5727 Necklace(全排列+二分图匹配)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 题意:现在有n个阳珠子和n个阴珠子,现在要把它们串成项链,要求是阴阳珠子间隔串,但是有些阴阳珠 ...