Untitled


SUBMITTED BY: Guest

DATE: Nov. 22, 2013, 8:37 p.m.

FORMAT: Text only

SIZE: 14.2 kB

HITS: 7614

  1. Sub Extract()
  2. Dim i As Long, j As Long, m As Long
  3. Dim strProject As String
  4. Dim RDate As Date
  5. Dim RVal As Single
  6. Dim BlnProjExists As Boolean
  7. With Sheets("Enhancements").Range("B3")
  8. For i = 1 To .CurrentRegion.Rows.Count - 1
  9. For j = 0 To 13
  10. .Offset(i, j) = ""
  11. Next j
  12. Next i
  13. End With
  14. With Sheets("AllData").Range("E3")
  15. For i = 1 To .CurrentRegion.Rows.Count - 1
  16. strProject = .Offset(i, 0)
  17. RDate = .Offset(i, 3)
  18. RVal = .Offset(i, 4)
  19. If InStr(.Offset(i, 0), "Enhancements") > 0 Then
  20. strProject = .Offset(i, 0)
  21. ElseIf InStr(.Offset(i, 0), "OVH") > 0 And RVal > 0 Then
  22. strProject = .Offset(i, -1)
  23. Else
  24. GoTo NextLoop
  25. End If
  26. With Sheets("Enhancements").Range("B3")
  27. If .CurrentRegion.Rows.Count = 1 Then
  28. .Offset(1, 0) = strProject
  29. j = 1
  30. Else
  31. BlnProjExists = False
  32. For j = 1 To .CurrentRegion.Rows.Count - 1
  33. If .Offset(j, 0) = strProject Then
  34. BlnProjExists = True
  35. Exit For
  36. End If
  37. Next j
  38. If BlnProjExists = False Then
  39. .Offset(j, 0) = strProject
  40. End If
  41. End If
  42. Select Case Format(RDate, "mmm yy")
  43. Case "Apr 13"
  44. m = 1
  45. Case "May 13"
  46. m = 2
  47. Case "Jun 13"
  48. m = 3
  49. Case "Jul 13"
  50. m = 4
  51. Case "Aug 13"
  52. m = 5
  53. Case "Sep 13"
  54. m = 6
  55. Case "Oct 13"
  56. m = 7
  57. Case "Nov 13"
  58. m = 8
  59. Case "Dec 13"
  60. m = 9
  61. Case "Jan 14"
  62. m = 10
  63. Case "Feb 14"
  64. m = 11
  65. Case "Mar 14"
  66. m = 12
  67. End Select
  68. .Offset(j, m) = .Offset(j, m) + RVal
  69. End With
  70. NextLoop:
  71. Next i
  72. End With
  73. End Sub
  74. If InStr(.Offset(i, 0), "Enhancements") > 0 Then
  75. strProject = .Offset(i, 0)
  76. ElseIf InStr(.Offset(i, 0), "OVH") > 0 And RVal > 0 Then
  77. strProject = .Offset(i, -1)
  78. Else
  79. GoTo NextLoop
  80. End If
  81. With Sheets("Enhancements").Range("B3")
  82. If .CurrentRegion.Rows.Count = 1 Then
  83. .Offset(1, 0) = strProject
  84. j = 1
  85. Else
  86. Sub HTH()
  87. Dim rLookup As Range, rFound As Range
  88. Dim lLastRow As Long, lRow As Long
  89. Dim lMonthIndex As Long, lProjectIndex As Long
  90. Dim vData As Variant, vMonths As Variant
  91. Dim iLoop As Integer
  92. Dim vbDict As Object
  93. With Worksheets("AllData")
  94. Set rLookup = .Range("E3", .Cells(Rows.Count, "E").End(xlUp))
  95. Set rFound = .Range("E3")
  96. End With
  97. Set vbDict = CreateObject("Scripting.Dictionary")
  98. vMonths = Array(4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3)
  99. For iLoop = 0 To 1
  100. lRow = 0: lLastRow = 3
  101. vbDict.RemoveAll: ReDim vData(rLookup.Count, 13)
  102. Do
  103. Set rFound = Worksheets("AllData").Cells.Find(Array("Enhancements", "OVH")(iLoop), _
  104. rFound, , , xlByRows, xlNext, False)
  105. If rFound Is Nothing Then Exit Do
  106. If rFound.Row <= lLastRow Then Exit Do
  107. lMonthIndex = WorksheetFunction.Match(Month(CDate(rFound.Offset(, 4).Value)), vMonths, False)
  108. If vbDict.exists(rFound.Offset(, -iLoop).Value) Then
  109. lProjectIndex = vbDict.Item(rFound.Value)
  110. vData(lProjectIndex, lMonthIndex) = _
  111. vData(lProjectIndex, lMonthIndex) + rFound.Offset(, 4).Value
  112. Else
  113. vbDict.Add rFound.Offset(, -iLoop).Value, lRow
  114. vData(lRow, 0) = rFound.Offset(, -iLoop).Value
  115. vData(lRow, lMonthIndex) = rFound.Offset(, 4).Value
  116. lRow = lRow + 1
  117. End If
  118. lLastRow = rFound.Row
  119. Loop
  120. If iLoop = 0 Then
  121. With Worksheets("Enhancements")
  122. .Range("B4:O" & Rows.Count).ClearContents
  123. .Range("B4").Resize(vbDict.Count + 1, 13).Value = vData
  124. End With
  125. Else
  126. With Worksheets("Overheads")
  127. .Range("B4:O" & Rows.Count).ClearContents
  128. .Range("B4").Resize(vbDict.Count + 1, 13).Value = vData
  129. End With
  130. End If
  131. Next iLoop
  132. End Sub
  133. Sub HTH()
  134. Dim rLookup As Range, rFound As Range
  135. Dim lLastRow As Long, lRow As Long
  136. Dim lMonthIndex As Long, lProjectIndex As Long
  137. Dim vData As Variant, vMonths As Variant
  138. Dim iLoop As Integer
  139. Dim vbDict As Object
  140. '// Get the projects range to loop through
  141. With Worksheets("AllData")
  142. Set rLookup = .Range("E3", .Cells(Rows.Count, "E").End(xlUp))
  143. Set rFound = .Range("E3")
  144. End With
  145. '// Use a latebinded dictionary to store the project names.
  146. Set vbDict = CreateObject("Scripting.Dictionary")
  147. '// Create an array of the months to get the correct columns. Instead of your select case method
  148. vMonths = Array(4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3)
  149. '// Loop through both search requirements
  150. For iLoop = 0 To 1
  151. '// Set the counters - lLastRow is used to make sure the loop is not never ending.
  152. lRow = 0: lLastRow = 3
  153. '// Clear the dictionary and create the projects array.
  154. vbDict.RemoveAll: ReDim vData(rLookup.Count, 13)
  155. Do
  156. '// Search using the criteria requried
  157. Set rFound = Worksheets("AllData").Cells.Find(Array("Enhancements", "OVH")(iLoop), _
  158. rFound, , , xlByRows, xlNext, False)
  159. '// Make sure something was found and its not a repeat.
  160. If rFound Is Nothing Then Exit Do
  161. If rFound.Row <= lLastRow Then Exit Do
  162. '// Get the correct month column using our months array and the project date.
  163. lMonthIndex = WorksheetFunction.Match(Month(CDate(rFound.Offset(, 4).Value)), vMonths, False)
  164. '// Check if the project exists.
  165. If vbDict.exists(rFound.Offset(, -iLoop).Value) Then
  166. '// Yes it exists so add the actuals to the correct project/month.
  167. lProjectIndex = vbDict.Item(rFound.Value)
  168. vData(lProjectIndex, lMonthIndex) = _
  169. vData(lProjectIndex, lMonthIndex) + rFound.Offset(, 4).Value
  170. Else
  171. '// No it doesnt exist, create it and then add the actuals to the correct project/month
  172. vbDict.Add rFound.Offset(, -iLoop).Value, lRow
  173. vData(lRow, 0) = rFound.Offset(, -iLoop).Value
  174. vData(lRow, lMonthIndex) = rFound.Offset(, 4).Value
  175. '// Increase the project count.
  176. lRow = lRow + 1
  177. End If
  178. '// Set the last row = the last found row to ensure we dont repeat the search.
  179. lLastRow = rFound.Row
  180. Loop
  181. If iLoop = 0 Then
  182. '// Clear the enhancements sheet and populate the cells from the array
  183. With Worksheets("Enhancements")
  184. .Range("B4:O" & Rows.Count).ClearContents
  185. .Range("B4").Resize(vbDict.Count + 1, 13).Value = vData
  186. End With
  187. Else
  188. '// Clear the overheads sheet and populate the cells from the array
  189. With Worksheets("Overheads")
  190. .Range("B4:O" & Rows.Count).ClearContents
  191. .Range("B4").Resize(vbDict.Count + 1, 13).Value = vData
  192. End With
  193. End If
  194. Next iLoop
  195. End Sub
  196. Sub Extract()
  197. Dim cllProjects As Collection
  198. Dim wsData As Worksheet
  199. Dim wsEnha As Worksheet
  200. Dim wsOver As Worksheet
  201. Dim rngFind As Range
  202. Dim rngFound As Range
  203. Dim rngProject As Range
  204. Dim arrProjects() As Variant
  205. Dim varProjectType As Variant
  206. Dim ProjectIndex As Long
  207. Dim cIndex As Long
  208. Dim dRVal As Double
  209. Dim dRDate As Double
  210. Dim strFirst As String
  211. Dim strProjectFirst As String
  212. Dim strProject As String
  213. Set wsData = Sheets("AllData")
  214. Set wsEnha = Sheets("Enhancements")
  215. Set wsOver = Sheets("Overheads")
  216. wsEnha.Range("B4:O" & Rows.Count).ClearContents
  217. wsOver.Range("B4:O" & Rows.Count).ClearContents
  218. With wsData.Range("E4", wsData.Cells(Rows.Count, "E").End(xlUp))
  219. If .Row < 4 Then Exit Sub 'No data
  220. On Error Resume Next
  221. For Each varProjectType In Array("Enhancements", "OVH")
  222. Set cllProjects = New Collection
  223. ProjectIndex = 0
  224. ReDim arrProjects(1 To WorksheetFunction.CountIf(.Cells, "*" & varProjectType & "*"), 1 To 14)
  225. Set rngFound = .Find(varProjectType, .Cells(.Cells.Count), xlValues, xlPart)
  226. If Not rngFound Is Nothing Then
  227. strFirst = rngFound.Address
  228. Do
  229. strProject = vbNullString
  230. dRDate = wsData.Cells(rngFound.Row, "H").Value2
  231. dRVal = wsData.Cells(rngFound.Row, "I").Value2
  232. If varProjectType = "OVH" And dRVal > 0 Then
  233. strProject = wsData.Cells(rngFound.Row, "D").Text
  234. Set rngFind = Intersect(.EntireRow, wsData.Columns("D"))
  235. ElseIf varProjectType = "Enhancements" Then
  236. strProject = wsData.Cells(rngFound.Row, "E").Text
  237. Set rngFind = .Cells
  238. End If
  239. If Len(strProject) > 0 Then
  240. cllProjects.Add LCase(strProject), LCase(strProject)
  241. If cllProjects.Count > ProjectIndex Then
  242. ProjectIndex = cllProjects.Count
  243. arrProjects(ProjectIndex, 1) = strProject
  244. Set rngProject = Intersect(rngFound.EntireRow, Columns(rngFind.Column))
  245. strProjectFirst = rngProject.Address
  246. Do
  247. If LCase(rngProject.Text) = LCase(strProject) Then
  248. dRDate = wsData.Cells(rngProject.Row, "H").Value2
  249. dRVal = wsData.Cells(rngProject.Row, "I").Value2
  250. cIndex = Month(dRDate) - 2 + (Year(dRDate) - 2013) * 12
  251. arrProjects(ProjectIndex, cIndex) = arrProjects(ProjectIndex, cIndex) + dRVal
  252. End If
  253. Set rngProject = rngFind.Find(arrProjects(ProjectIndex, 1), rngProject, xlValues, xlPart)
  254. Loop While rngProject.Address <> strProjectFirst
  255. End If
  256. End If
  257. Set rngFound = .Find(varProjectType, rngFound, xlValues, xlPart)
  258. Loop While rngFound.Address <> strFirst
  259. End If
  260. If cllProjects.Count > 0 Then
  261. Select Case varProjectType
  262. Case "Enhancements": wsEnha.Range("B4").Resize(cllProjects.Count, UBound(arrProjects, 2)).Value = arrProjects
  263. Case "OVH": wsOver.Range("B4").Resize(cllProjects.Count, UBound(arrProjects, 2)).Value = arrProjects
  264. End Select
  265. Set cllProjects = Nothing
  266. End If
  267. Next varProjectType
  268. On Error GoTo 0
  269. End With
  270. Set cllProjects = Nothing
  271. Set wsData = Nothing
  272. Set wsEnha = Nothing
  273. Set wsOver = Nothing
  274. Set rngFound = Nothing
  275. Set rngProject = Nothing
  276. Erase arrProjects
  277. End Sub

comments powered by Disqus