Advertisement
Guest User

Untitled

a guest
Aug 24th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.24 KB | None | 0 0
  1. Option Explicit
  2.  
  3. '/ Workbooks
  4. Public WbSubsheet As Workbook '/ Contains all Lumin Wealth submitted Business
  5. Public WbAdviserReport As Workbook '/ Will Contain an aggregation of the subsheet and a submission report (by month) for each adviser
  6.  
  7. '/ Adviser Report worksheets
  8. Public WsAggregatedData As Worksheet '/ Will contain the aggregated subsheet data
  9. Public WsAdviserReport As Worksheet '/ Will contain the submissions report, reported by Adviser
  10. Public WsProviderReport As Worksheet '/ Will contain the submissions report, reported by Provider
  11. Public WsProductReport As Worksheet '/ Will contain the submissions report, reportrd by Type of Business
  12.  
  13. '/ Subsheet Worksheets
  14. Public WsNewClient As Worksheet '/ 'New Client' Investments of Assets
  15. Public WsExistingClient As Worksheet '/ 'Existing Client' Investments of assets
  16. Public WsGroupSchemes As Worksheet '/ 'e.g. Corporate pension schemes and/or Auto Enrolment
  17. Public WsOther As Worksheet '/ Misc. bits and pieces
  18. Public WsMcOngoing As Worksheet '/ Martin's recurring product commissions e.g. insurance policies
  19. Public WsJhOngoing As Worksheet '/ Jon's recurring product commissions e.g. insurance policies
  20. Public WsAegonQuilterArc As Worksheet '/ Recurring fees from accounts with Aegon, Quilter and ARC
  21. Public WsAscentric As Worksheet '/ Recurring fees from accounts on Asccentric
  22.  
  23. '/ Data Arrays
  24. Public ArrAggregatedArrays As Variant '/ Holds all the sheet-Data Arrays
  25.  
  26. Public ArrAggregatedData As Variant '/ The data from all worksheets
  27.  
  28. Public ArrProviders As Variant '/ all providers found in the subsheet
  29. Public ArrAdvisers As Variant '/ all the advisers found in the subsheet
  30.  
  31. '/ Collections of names
  32. Public ColAllHeadings As Collection '/ All desired Column Headings from the subsheet
  33. Public ColMetrics As Collection '/ Metrics in the final report
  34.  
  35. '/ Constants, and variables that are only set once
  36. Public StrCurrentDate As String '/ The current Date for datestamping the saved report
  37. Public StrSavedReportFilename As String '/ The filename to save the report as
  38.  
  39. Public Const StrAdviserReportFilePath As String = "S:Lumin Admin DocsAdviser Submission Reports" '/ The path of the folder containing the Adviser Report
  40. Public Const StrSavedReportsFilePath As String = "S:Lumin Admin DocsAdviser Submission ReportsSaved Reports" '/ The path of the folder containing previous reports
  41. Public Const StrSubsheetFilePath As String = "S:Lumin Admin DocsSubsheet and Commission statements" '/ The path of the folder containing the Subsheet
  42.  
  43. Public Const StrAdviserReportFilename As String = "Adviser Submissions Report - v0.3.xlsm" '/ The filename of the Adviser Submissions Report
  44. Public Const StrSubsheetFilename As String = "Lumin Subsheet 2015.xlsm" '/ The filename of the Subsheet
  45.  
  46. Public Const LngFinalCellRow As Long = 1048576
  47. Public Const LngFinalCellColumn As Long = 16384
  48.  
  49. Option Explicit
  50. Option Compare Text
  51.  
  52. Public Sub GenerateAdviserSubmissionReports()
  53.  
  54. Dim varScreenUpdating As Variant
  55. Dim varEnableEvents As Variant
  56. Dim varCalculation As Variant
  57.  
  58. Call StoreApplicationSettings(varScreenUpdating, varEnableEvents, varCalculation)
  59. Call DisableApplicationSettings
  60.  
  61. '/======================================================================================================================================================
  62. '/ Author: Zak Armstrong
  63. '/ Email: zak.armstrong@luminwealth.co.uk
  64. '/ Date: 21/August/2015
  65. '/ Version: 0.3
  66. '/
  67. '/ Description: All Lumin Wealth Business is contained in the Subsheet. This macro produces adviser totals for business (assets and fees) in the previous year
  68. '/ (month by month breakdown) by aggregating the subsheet into one giant table and then assigning each piece of business to an adviser, a Month and a business type.
  69. '/ The report can then be easily configured for any desired outputs (E.G. by adviser, by provider, by type of business)
  70. '/======================================================================================================================================================
  71. Dim arrNewClient As Variant '/ all data on the "New Client Investment" Sheet
  72. arrNewClient = Array()
  73.  
  74. Dim arrExistingClient As Variant '/ all data on the "Existing Client Investment" Sheet
  75. arrExistingClient = Array()
  76.  
  77. Dim arrGroupSchemes As Variant '/ all data on the "Group Schemes" Sheet
  78. arrGroupSchemes = Array()
  79.  
  80. Dim arrOther As Variant '/ all data on the "Other" Sheet
  81. arrOther = Array()
  82.  
  83. Dim arrMcOngoing As Variant '/ all data on the "MC Ongoing" Sheet
  84. arrMcOngoing = Array()
  85.  
  86. Dim arrJhOngoing As Variant '/ all data on the "JH Ongoing" Sheet
  87. arrJhOngoing = Array()
  88.  
  89. Dim arrAegonQuilterArc As Variant '/ all data on the "AG-QU-ARC" Sheet
  90. arrAegonQuilterArc = Array()
  91.  
  92. Dim arrAscentric As Variant '/ all data on the "Ascentric" Sheet
  93. arrAscentric = Array()
  94.  
  95. Dim i As Long '/ General counters
  96. Dim j As Long '/
  97. Dim k As Long '/
  98. '/======================================================================================================================================================
  99. Call InitialiseStuff
  100.  
  101. '/==================================================
  102. '/ Get all sheet data into arrays
  103. '/==================================================
  104.  
  105. Dim strTopLeftCellIdentifier As String
  106. strTopLeftCellIdentifier = "Adviser"
  107.  
  108. Call PutSheetDataInArray(WbSubsheet, WsNewClient, arrNewClient, strTopLeftCellIdentifier)
  109. Call PutSheetDataInArray(WbSubsheet, WsExistingClient, arrExistingClient, strTopLeftCellIdentifier)
  110. Call PutSheetDataInArray(WbSubsheet, WsGroupSchemes, arrGroupSchemes, strTopLeftCellIdentifier)
  111. Call PutSheetDataInArray(WbSubsheet, WsOther, arrOther, strTopLeftCellIdentifier)
  112. Call PutSheetDataInArray(WbSubsheet, WsMcOngoing, arrMcOngoing, strTopLeftCellIdentifier)
  113. Call PutSheetDataInArray(WbSubsheet, WsJhOngoing, arrJhOngoing, strTopLeftCellIdentifier)
  114. Call PutSheetDataInArray(WbSubsheet, WsAegonQuilterArc, arrAegonQuilterArc, strTopLeftCellIdentifier)
  115.  
  116. strTopLeftCellIdentifier = "Account No"
  117. Call PutSheetDataInArray(WbSubsheet, WsAscentric, arrAscentric, strTopLeftCellIdentifier)
  118.  
  119. Call InsertAscentricLifeCoColumn(arrAscentric)
  120.  
  121. ReDim ArrAggregatedArrays(1 To 8)
  122. ArrAggregatedArrays(1) = arrNewClient
  123. ArrAggregatedArrays(2) = arrExistingClient
  124. ArrAggregatedArrays(3) = arrGroupSchemes
  125. ArrAggregatedArrays(4) = arrOther
  126. ArrAggregatedArrays(5) = arrMcOngoing
  127. ArrAggregatedArrays(6) = arrJhOngoing
  128. ArrAggregatedArrays(7) = arrAegonQuilterArc
  129. ArrAggregatedArrays(8) = arrAscentric
  130.  
  131. '/==================================================
  132. '/ Filter sheet data for desired columns
  133. '/==================================================
  134. For i = LBound(ArrAggregatedArrays) To UBound(ArrAggregatedArrays)
  135. Call FilterSheetArrayForColumns(ArrAggregatedArrays(i))
  136. Next i
  137.  
  138. '/==================================================
  139. '/ Aggregate Data
  140. '/==================================================
  141. Call AggregateSheetData
  142.  
  143. Application.DisplayAlerts = False
  144. WbSubsheet.Close
  145. Application.DisplayAlerts = True
  146.  
  147. '/==================================================
  148. '/ Print Data
  149. '/==================================================
  150.  
  151. Dim rngStartCell As Range
  152. Set rngStartCell = WsAggregatedData.Cells(1, 1)
  153.  
  154. Call Print2dArrayToSheet(WbAdviserReport, WsAggregatedData, ArrAggregatedData, rngStartCell)
  155.  
  156. Call RestoreApplicationSettings(varScreenUpdating, varEnableEvents, varCalculation)
  157.  
  158. End Sub
  159.  
  160. Public Sub FilterSheetArrayForColumns(ByRef arrCurrentArray As Variant)
  161.  
  162. '/======================================================================================================================================================
  163. '/ Author: Zak Armstrong
  164. '/ Email: zak.armstrong@luminwealth.co.uk
  165. '/ Date: 12/August/2015
  166. '/
  167. '/ Description: Takes Sheet arrays, finds the columns from the colAllHeadings, recreates the array with just that data (and empty columns for the ones not found)
  168. '/======================================================================================================================================================
  169. Dim i As Long
  170. Dim j As Long
  171. Dim k As Long
  172.  
  173. Dim lngFinalRow As Long
  174. Dim lngFinalColumn As Long
  175.  
  176. Dim arrTempArray As Variant '/ Temporarily holds the filtered information
  177. arrTempArray = Array()
  178.  
  179. Dim arrHeadingsRow As Variant '/ Holds the top (headings) row for application.match
  180. arrHeadingsRow = Array()
  181.  
  182. Dim varColumnPosition As Variant '/ Holds the position of the relevant column
  183.  
  184. Dim strHeading As String '/ The current heading to search for
  185. '/======================================================================================================================================================
  186.  
  187. Call AssignArrayBounds(arrCurrentArray:=arrCurrentArray, UB1:=lngFinalRow, UB2:=lngFinalColumn)
  188.  
  189. '/==================================================
  190. '/ Recreate Headings Row
  191. '/==================================================
  192. ReDim arrHeadingsRow(1 To lngFinalColumn)
  193.  
  194. For i = 1 To lngFinalColumn
  195. arrHeadingsRow(i) = arrCurrentArray(1, i)
  196. Next i
  197.  
  198. '/==================================================
  199. '/ Find Columns, put in array
  200. '/==================================================
  201. ReDim arrTempArray(0 To lngFinalRow, 0 To ColAllHeadings.Count)
  202. arrTempArray(0, 0) = arrCurrentArray(0, 0)
  203.  
  204. Dim lngDestinationColumn As Long
  205. Dim lngSourceColumn As Long
  206.  
  207. For i = 1 To ColAllHeadings.Count
  208. strHeading = ColAllHeadings(i)
  209. varColumnPosition = Application.Match(strHeading, arrHeadingsRow, 0)
  210.  
  211. If IsError(varColumnPosition) _
  212. Then
  213. Call MissingDataHeadingsHandler(arrCurrentArray, strHeading)
  214. Else
  215. lngDestinationColumn = i
  216. lngSourceColumn = varColumnPosition
  217.  
  218. Call CopyColumn2d(arrCurrentArray, arrTempArray, lngSourceColumn, lngDestinationColumn)
  219. End If
  220. Next i
  221.  
  222. Call CopyArrayContents2d(arrTempArray, arrCurrentArray)
  223.  
  224. End Sub
  225.  
  226. Public Sub AggregateSheetData()
  227.  
  228. '/======================================================================================================================================================
  229. '/ Author: Zak Armstrong
  230. '/ Email: zak.armstrong@luminwealth.co.uk
  231. '/ Date: 13/August/2015
  232. '/
  233. '/ Description: For Each array, add the data to arrAggregatedData
  234. '/======================================================================================================================================================
  235. Dim rngTopLeftCell As Range
  236.  
  237. Dim lngFirstRow As Long
  238. Dim lngFirstColumn As Long
  239.  
  240. Dim lngCurrentRow As Long
  241.  
  242. Dim lngFinalRow As Long
  243. Dim lngFinalColumn As Long
  244.  
  245. Dim i As Long
  246. Dim j As Long
  247. Dim k As Long
  248.  
  249. Dim rngTableRange As Range
  250.  
  251. Dim arrTransposedData() As Variant
  252.  
  253. Dim strHolder As String
  254.  
  255. Dim LB1 As Long
  256. Dim UB1 As Long
  257. Dim LB2 As Long
  258. Dim UB2 As Long
  259. '/======================================================================================================================================================
  260.  
  261. '/==================================================
  262. '/ Aggregate Data
  263. '/==================================================
  264.  
  265. lngCurrentRow = 1
  266. ReDim ArrAggregatedData(1 To ColAllHeadings.Count, 1 To 1)
  267.  
  268. For i = 1 To ColAllHeadings.Count
  269. ArrAggregatedData(i, 1) = ColAllHeadings(i)
  270. Next i
  271.  
  272. For i = LBound(ArrAggregatedArrays) To UBound(ArrAggregatedArrays)
  273.  
  274. Call AssignArrayBounds(ArrAggregatedArrays(i), LB1, UB1, LB2, UB2)
  275.  
  276. For j = LB1 + 2 To UB1
  277. lngCurrentRow = lngCurrentRow + 1
  278. ReDim Preserve ArrAggregatedData(1 To ColAllHeadings.Count, 1 To lngCurrentRow)
  279.  
  280. For k = LB2 + 1 To UB2
  281. ArrAggregatedData(k, lngCurrentRow) = ArrAggregatedArrays(i)(j, k)
  282. Next k
  283. Next j
  284. Next i
  285.  
  286. '/==================================================
  287. '/ Transpose Data
  288. '/==================================================
  289. Call Transpose2dArray(ArrAggregatedData)
  290.  
  291. '/==================================================
  292. '/ Print to sheet
  293. '/==================================================
  294. Call Print2dArrayToSheet(wbTarget, wsTarget, arrData, rngStartCell)
  295.  
  296. End Sub
  297.  
  298. Option Explicit
  299. Option Compare Text
  300.  
  301. Public Sub InitialiseStuff()
  302.  
  303. '/ initialise public arrays
  304. ArrAggregatedData = Array()
  305. ArrAggregatedArrays = Array()
  306. ArrProviders = Array()
  307. ArrAdvisers = Array()
  308.  
  309. Call GetWorkbook(StrAdviserReportFilename, StrAdviserReportFilePath)
  310. Set WbAdviserReport = Workbooks(StrAdviserReportFilename)
  311.  
  312. Call GetWorkbook(StrSubsheetFilename, StrSubsheetFilePath)
  313. Set WbSubsheet = Workbooks(StrSubsheetFilename)
  314.  
  315. Call AssignWorksheets
  316.  
  317. Call InitialiseCollections
  318.  
  319. End Sub
  320.  
  321. Public Sub InsertAscentricLifeCoColumn(ByRef arrAscentric As Variant)
  322.  
  323. '/======================================================================================================================================================
  324. '/ Author: Zak Armstrong
  325. '/ Email: zak.armstrong@luminwealth.co.uk
  326. '/ Date: 17/August/2015
  327. '/
  328. '/ Description: Inserts a column in the ascentric data array called "Life Co" and filled with "Ascentric" for easy identification later
  329. '/======================================================================================================================================================
  330. Dim i As Long
  331. Dim j As Long
  332. Dim k As Long
  333.  
  334. Dim LB1 As Long, UB1 As Long
  335. Dim LB2 As Long, UB2 As Long
  336. '/======================================================================================================================================================
  337.  
  338. Call AssignArrayBounds(arrAscentric, LB1, UB1, LB2, UB2)
  339.  
  340. ReDim Preserve arrAscentric(LB1 To UB1, LB2 To UB2 + 1)
  341.  
  342. arrAscentric(LB1 + 1, UB2 + 1) = "Life Co"
  343.  
  344. For i = LB1 + 2 To UB1
  345. arrAscentric(i, UB2 + 1) = "Ascentric"
  346. Next i
  347.  
  348. End Sub
  349.  
  350.  
  351.  
  352. Public Sub MissingDataHeadingsHandler(ByRef arrCurrentArray As Variant, ByVal strHeading As String)
  353.  
  354. '/======================================================================================================================================================
  355. '/ Author: Zak Armstrong
  356. '/ Email: zak.armstrong@luminwealth.co.uk
  357. '/ Date: 13/August/2015
  358. '/
  359. '/ Description: Handle instances where a column heading can't be found. Reference against sheet-specific lists to see if the column should be there or not.
  360. '/======================================================================================================================================================
  361. Dim bErrorFound As Boolean
  362.  
  363. Dim colMissingSheetHeadings As Collection '/ For each sheet, contains the headings that shouldn't be there
  364.  
  365. Dim strException As String '/ holds string items from colMissingSheetHeadings
  366. Dim strErrorMessage As String
  367.  
  368. Dim i As Long
  369. Dim j As Long
  370. Dim k As Long
  371. '/======================================================================================================================================================
  372.  
  373. strErrorMessage = "Couldn't find Column Heading: " & arrCurrentArray(0, 0) & ": " & strHeading
  374. bErrorFound = True
  375.  
  376. Set colMissingSheetHeadings = New Collection
  377.  
  378. Select Case arrCurrentArray(0, 0) '/ Contains the name of the worksheet the data was taken from
  379.  
  380. Case Is = WsNewClient.Name
  381. Call InitialiseNewClientHeadingsExceptions(colMissingSheetHeadings)
  382.  
  383. Case Is = WsExistingClient.Name
  384. Call InitialiseExistingClientHeadingsExceptions(colMissingSheetHeadings)
  385.  
  386.  
  387. Case Is = WsGroupSchemes.Name
  388. Call InitialiseGroupSchemesHeadingsExceptions(colMissingSheetHeadings)
  389.  
  390.  
  391. Case Is = WsOther.Name
  392. Call InitialiseOtherHeadingsExceptions(colMissingSheetHeadings)
  393.  
  394.  
  395. Case Is = WsMcOngoing.Name
  396. Call InitialiseMcOngoingHeadingsExceptions(colMissingSheetHeadings)
  397.  
  398.  
  399. Case Is = WsJhOngoing.Name
  400. Call InitialiseJhOngoingHeadingsExceptions(colMissingSheetHeadings)
  401.  
  402.  
  403. Case Is = WsAegonQuilterArc.Name
  404. Call InitialiseAegonQuilterArcHeadingsExceptions(colMissingSheetHeadings)
  405.  
  406.  
  407. Case Is = WsAscentric.Name
  408. Call InitialiseAscentricHeadingsExceptions(colMissingSheetHeadings)
  409.  
  410. Case Else
  411. Call ErrorMessage(strErrorMessage)
  412.  
  413. End Select
  414.  
  415.  
  416. For i = 1 To colMissingSheetHeadings.Count
  417. strException = colMissingSheetHeadings(i)
  418. If strHeading = strException Then bErrorFound = False
  419. Next i
  420.  
  421. If bErrorFound = True Then Call ErrorMessage(strErrorMessage)
  422.  
  423. End Sub
  424.  
  425. Option Explicit
  426. Option Compare Text
  427.  
  428. Public Sub AssignWorksheets()
  429.  
  430. '/======================================================================================================================================================
  431. '/ Date: 21.08.2015
  432. '/======================================================================================================================================================
  433.  
  434. WbAdviserReport.Activate
  435.  
  436. Set WsAggregatedData = WbAdviserReport.Worksheets("Aggregated Subsheet Data")
  437. Set WsAdviserReport = WbAdviserReport.Worksheets("Adviser Submissions Report")
  438. Set WsProviderReport = WbAdviserReport.Worksheets("Provider Submissions Report")
  439. Set WsProductReport = WbAdviserReport.Worksheets("Product Submissions Report")
  440.  
  441. WbSubsheet.Activate
  442.  
  443. Set WsNewClient = WbSubsheet.Worksheets("New Client Investment")
  444. Set WsExistingClient = WbSubsheet.Worksheets("Existing Client Investment")
  445. Set WsGroupSchemes = WbSubsheet.Worksheets("Group Schemes")
  446. Set WsOther = WbSubsheet.Worksheets("Other")
  447. Set WsMcOngoing = WbSubsheet.Worksheets("MC Ongoing")
  448. Set WsJhOngoing = WbSubsheet.Worksheets("JH Ongoing")
  449. Set WsAegonQuilterArc = WbSubsheet.Worksheets("AG-QU-ARC")
  450. Set WsAscentric = WbSubsheet.Worksheets("Ascentric")
  451.  
  452. End Sub
  453.  
  454. Public Sub InitialiseCollections()
  455.  
  456. '/======================================================================================================================================================
  457. '/ Date: 21.08.2015
  458. '/======================================================================================================================================================
  459. Dim i As Long
  460. '/======================================================================================================================================================
  461.  
  462. Set ColAllHeadings = New Collection
  463. '/ N.B. this will be the order of headings in the aggregated sheet
  464.  
  465. ColAllHeadings.Add ("Adviser")
  466.  
  467. ColAllHeadings.Add ("First Name")
  468. ColAllHeadings.Add ("Last Name")
  469. ColAllHeadings.Add ("Account Name")
  470. ColAllHeadings.Add ("Life Co")
  471. ColAllHeadings.Add ("Date Submitted")
  472. ColAllHeadings.Add ("Description")
  473.  
  474. ColAllHeadings.Add ("Investment Amount")
  475. ColAllHeadings.Add ("Money Received")
  476.  
  477. ColAllHeadings.Add ("Total Monthly Premium")
  478. ColAllHeadings.Add ("Single Premium")
  479.  
  480. ColAllHeadings.Add ("Commission Due")
  481. ColAllHeadings.Add ("Comm Paid - Checked To Bank")
  482. ColAllHeadings.Add ("Date Received - Bank")
  483.  
  484. For i = 1 To 12
  485. ColAllHeadings.Add (DateValue("01/" & Right("0" & i, 2) & "/" & Year(Date)))
  486. Next i
  487.  
  488.  
  489.  
  490. Set ColMetrics = New Collection
  491. ColMetrics.Add ("Investment Amount")
  492. ColMetrics.Add ("Single Premium")
  493. ColMetrics.Add ("Total Monthly Premium")
  494. ColMetrics.Add ("Commission Due")
  495. ColMetrics.Add ("Comm Paid - Checked To Bank")
  496. ColMetrics.Add ("Recurring")
  497.  
  498. End Sub
  499.  
  500. Public Sub InitialiseNewClientHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  501.  
  502. Dim i As Long
  503.  
  504. colMissingSheetHeadings.Add ("Total Monthly Premium")
  505. colMissingSheetHeadings.Add ("Single Premium")
  506. colMissingSheetHeadings.Add ("Account Name")
  507. colMissingSheetHeadings.Add ("Life Co")
  508. For i = 1 To 12
  509. colMissingSheetHeadings.Add (DateValue("01/" & Right("0" & i, 2) & "/" & Year(Date)))
  510. Next i
  511.  
  512. End Sub
  513.  
  514. Public Sub InitialiseExistingClientHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  515.  
  516. '/ Different List of names
  517.  
  518. End Sub
  519.  
  520. Public Sub InitialiseGroupSchemesHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  521.  
  522. '/ Different List of names
  523.  
  524. End Sub
  525.  
  526. Public Sub InitialiseOtherHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  527.  
  528. '/ Different List of names
  529.  
  530. End Sub
  531.  
  532. Public Sub InitialiseMcOngoingHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  533.  
  534. '/ Different List of names
  535.  
  536. End Sub
  537.  
  538. Public Sub InitialiseJhOngoingHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  539.  
  540. '/ Different List of names
  541.  
  542. End Sub
  543.  
  544. Public Sub InitialiseAegonQuilterArcHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  545.  
  546. '/ Different List of names
  547.  
  548. End Sub
  549.  
  550. Public Sub InitialiseAscentricHeadingsExceptions(ByRef colMissingSheetHeadings As Collection)
  551.  
  552. '/ Different List of names
  553. End Sub
  554.  
  555. Option Explicit
  556. Option Compare Text
  557.  
  558. Public Function IsWorkbookOpen(ByVal strTargetName As String) As Boolean
  559.  
  560. On Error Resume Next
  561.  
  562. Workbooks(strTargetName).Activate
  563.  
  564. If ActiveWorkbook.Name <> strTargetName _
  565. Then
  566. IsWorkbookOpen = False
  567. Else
  568. IsWorkbookOpen = True
  569. End If
  570.  
  571. On Error GoTo 0
  572.  
  573. End Function
  574.  
  575. Public Sub PutSheetDataInArray(ByRef wbCurrentWorkbook As Workbook, ByRef wsCurrentWorksheet As Worksheet, ByRef arrCurrentArray As Variant, ByVal strTopLeftCellIdentifier As String, _
  576. Optional ByVal lngStartRow As Long = 1, Optional ByVal lngEndRow As Long = 10, _
  577. Optional ByVal lngStartColumn As Long = 1, Optional ByVal lngEndColumn As Long = 10)
  578.  
  579. '/======================================================================================================================================================
  580. '/ Author: Zak Armstrong
  581. '/ Email: zak.armstrong@luminwealth.co.uk
  582. '/ Date: 21/August/2015
  583. '/======================================================================================================================================================
  584. Dim i As Long
  585. Dim j As Long
  586. Dim k As Long
  587.  
  588. Dim lngFirstRow As Long
  589. Dim lngFirstColumn As Long
  590. Dim lngFinalRow As Long
  591. Dim lngFinalColumn As Long
  592.  
  593. Dim rngTopLeftCell As Range
  594. Dim rngSearchRange As Range
  595.  
  596. Dim strErrorMessage As String
  597. '/======================================================================================================================================================
  598. '/==================================================
  599. '/ Open Worksheet
  600. '/==================================================
  601. wbCurrentWorkbook.Activate
  602. wsCurrentWorksheet.Activate
  603. wsCurrentWorksheet.Cells.EntireRow.Hidden = False
  604.  
  605. '/==================================================
  606. '/ Find TopLeftCell
  607. '/==================================================
  608. If IsMissing(lngEndRow) Then lngEndRow = wsCurrentWorksheet.Rows.Count
  609. If IsMissing(lngEndColumn) Then lngEndColumn = wsCurrentWorksheet.Columns.Count
  610.  
  611. Set rngSearchRange = wsCurrentWorksheet.Range(Cells(lngStartRow, lngStartColumn), Cells(lngEndRow, lngEndColumn))
  612. Set rngTopLeftCell = rngSearchRange.Find(strTopLeftCellIdentifier, LookIn:=xlValues)
  613.  
  614. If rngTopLeftCell Is Nothing _
  615. Then
  616. strErrorMessage = "Couldn't find cell """ & strTopLeftCellIdentifier & """ in " & wsCurrentWorksheet.Name
  617. Call ErrorMessage(strErrorMessage)
  618. End If
  619.  
  620. '/==================================================
  621. '/ Determine range of data, pass to array
  622. '/==================================================
  623. lngFirstRow = rngTopLeftCell.Row
  624. lngFirstColumn = rngTopLeftCell.Column
  625.  
  626. lngFinalRow = Cells(LngFinalCellRow, lngFirstColumn).End(xlUp).Row
  627. lngFinalColumn = Cells(lngFirstRow, LngFinalCellColumn).End(xlToLeft).Column
  628.  
  629. ReDim arrCurrentArray(0 To lngFinalRow - lngFirstRow + 1, 0 To lngFinalColumn - lngFirstColumn + 1)
  630. arrCurrentArray(0, 0) = wsCurrentWorksheet.Name
  631.  
  632. For i = lngFirstRow To lngFinalRow
  633. For j = lngFirstColumn To lngFinalColumn
  634. arrCurrentArray(i - lngFirstRow + 1, j - lngFirstColumn + 1) = wsCurrentWorksheet.Cells(i, j)
  635. Next j
  636. Next i
  637. End Sub
  638.  
  639. Public Sub CopyArrayContents5d(ByRef arrSource As Variant, ByRef arrDestination As Variant)
  640.  
  641. Dim LB1 As Long, UB1 As Long
  642. Dim LB2 As Long, UB2 As Long
  643. Dim LB3 As Long, UB3 As Long
  644. Dim LB4 As Long, UB4 As Long
  645. Dim LB5 As Long, UB5 As Long
  646.  
  647. Dim i As Long
  648. Dim j As Long
  649. Dim k As Long
  650. Dim l As Long
  651. Dim m As Long
  652.  
  653. Call AssignArrayBounds(arrSource, LB1, UB1, LB2, UB2, LB3, UB3, LB4, UB4, LB5, UB5)
  654.  
  655. Erase arrDestination
  656. ReDim arrDestination(LB1 To UB1, LB2 To UB2, LB3 To UB3, LB4 To UB4, LB5 To UB5)
  657.  
  658. For i = LB1 To UB1
  659. For j = LB2 To UB2
  660. For k = LB3 To UB3
  661. For l = LB4 To UB4
  662. For m = LB5 To UB5
  663. arrDestination(i, j, k, l, m) = arrSource(i, j, k, l, m)
  664. Next m
  665. Next l
  666. Next k
  667. Next j
  668. Next i
  669.  
  670. End Sub
  671.  
  672. Public Sub CopyArrayContents4d(ByRef arrSource As Variant, ByRef arrDestination As Variant)
  673.  
  674. Dim LB1 As Long, UB1 As Long
  675. Dim LB2 As Long, UB2 As Long
  676. Dim LB3 As Long, UB3 As Long
  677. Dim LB4 As Long, UB4 As Long
  678.  
  679. Dim i As Long
  680. Dim j As Long
  681. Dim k As Long
  682. Dim l As Long
  683.  
  684. Call AssignArrayBounds(arrSource, LB1, UB1, LB2, UB2, LB3, UB3, LB4, UB4)
  685.  
  686. Erase arrDestination
  687. ReDim arrDestination(LB1 To UB1, LB2 To UB2, LB3 To UB3, LB4 To UB4)
  688.  
  689. For i = LB1 To UB1
  690. For j = LB2 To UB2
  691. For k = LB3 To UB3
  692. For l = LB4 To UB4
  693. arrDestination(i, j, k, l) = arrSource(i, j, k, l)
  694. Next l
  695. Next k
  696. Next j
  697. Next i
  698.  
  699. End Sub
  700.  
  701. Public Sub CopyArrayContents3d(ByRef arrSource As Variant, ByRef arrDestination As Variant)
  702.  
  703. Dim LB1 As Long, UB1 As Long
  704. Dim LB2 As Long, UB2 As Long
  705. Dim LB3 As Long, UB3 As Long
  706.  
  707. Dim i As Long
  708. Dim j As Long
  709. Dim k As Long
  710.  
  711. Call AssignArrayBounds(arrSource, LB1, UB1, LB2, UB2, LB3, UB3)
  712.  
  713. Erase arrDestination
  714. ReDim arrDestination(LB1 To UB1, LB2 To UB2, LB3 To UB3)
  715.  
  716. For i = LB1 To UB1
  717. For j = LB2 To UB2
  718. For k = LB3 To UB3
  719. arrDestination(i, j, k) = arrSource(i, j, k)
  720. Next k
  721. Next j
  722. Next i
  723.  
  724. End Sub
  725.  
  726. Public Sub CopyArrayContents2d(ByRef arrSource As Variant, ByRef arrDestination As Variant)
  727.  
  728. Dim LB1 As Long, UB1 As Long
  729. Dim LB2 As Long, UB2 As Long
  730.  
  731. Dim i As Long
  732. Dim j As Long
  733.  
  734. Call AssignArrayBounds(arrSource, LB1, UB1, LB2, UB2)
  735.  
  736. Erase arrDestination
  737. ReDim arrDestination(LB1 To UB1, LB2 To UB2)
  738.  
  739. For i = LB1 To UB1
  740. For j = LB2 To UB2
  741. arrDestination(i, j) = arrSource(i, j)
  742. Next j
  743. Next i
  744.  
  745. End Sub
  746.  
  747. Public Sub CopyArrayContents1d(ByRef arrSource As Variant, ByRef arrDestination As Variant)
  748.  
  749. Dim LB1 As Long, UB1 As Long
  750.  
  751. Dim i As Long
  752.  
  753. Call AssignArrayBounds(arrSource, LB1, UB1)
  754.  
  755. Erase arrDestination
  756. ReDim arrDestination(LB1 To UB1)
  757.  
  758. For i = LB1 To UB1
  759. arrDestination(i) = arrSource(i)
  760. Next i
  761.  
  762. End Sub
  763.  
  764. Public Sub AssignArrayBounds(ByRef arrCurrentArray As Variant, _
  765. Optional ByRef LB1 As Variant, Optional ByRef UB1 As Variant, _
  766. Optional ByRef LB2 As Variant, Optional ByRef UB2 As Variant, _
  767. Optional ByRef LB3 As Variant, Optional ByRef UB3 As Variant, _
  768. Optional ByRef LB4 As Variant, Optional ByRef UB4 As Variant, _
  769. Optional ByRef LB5 As Variant, Optional ByRef UB5 As Variant)
  770.  
  771. If Not IsMissing(LB1) Then LB1 = LBound(arrCurrentArray, 1)
  772. If Not IsMissing(UB1) Then UB1 = UBound(arrCurrentArray, 1)
  773. If Not IsMissing(LB2) Then LB2 = LBound(arrCurrentArray, 2)
  774. If Not IsMissing(UB2) Then UB2 = UBound(arrCurrentArray, 2)
  775. If Not IsMissing(LB3) Then LB3 = LBound(arrCurrentArray, 3)
  776. If Not IsMissing(UB3) Then UB3 = UBound(arrCurrentArray, 3)
  777. If Not IsMissing(LB4) Then LB4 = LBound(arrCurrentArray, 4)
  778. If Not IsMissing(UB4) Then UB4 = UBound(arrCurrentArray, 4)
  779. If Not IsMissing(LB5) Then LB5 = LBound(arrCurrentArray, 5)
  780. If Not IsMissing(UB5) Then UB5 = UBound(arrCurrentArray, 5)
  781.  
  782. End Sub
  783.  
  784. Public Sub ErrorMessage(ByVal strErrorMessage As String)
  785.  
  786. MsgBox strErrorMessage
  787. Debug.Print strErrorMessage
  788. Call RestoreApplicationSettings(varScreenUpdating, varEnableEvents, varCalculation)
  789. End
  790.  
  791. End Sub
  792.  
  793. Public Sub Transpose2dArray(ByRef arrCurrentArray As Variant)
  794.  
  795. Dim LB1 As Long, UB1 As Long
  796. Dim LB2 As Long, UB2 As Long
  797.  
  798. Dim i As Long
  799. Dim j As Long
  800.  
  801. Call AssignArrayBounds(arrCurrentArray, LB1, UB1, LB2, UB2)
  802.  
  803. Dim arrTransposedArray() As Variant
  804. ReDim arrTransposedArray(LB2 To UB2, LB1 To UB1)
  805.  
  806. For i = LB1 To UB1
  807. For j = LB2 To UB2
  808. arrTransposedArray(j, i) = arrCurrentArray(i, j)
  809. Next j
  810. Next i
  811.  
  812. Erase arrCurrentArray
  813. ReDim arrCurrentArray(LB2 To UB2, LB1 To UB1)
  814. Call CopyArrayContents2d(arrTransposedArray, arrCurrentArray)
  815. End Sub
  816.  
  817. Public Sub StoreApplicationSettings(ByRef varScreenUpdating As Variant, ByRef varEnableEvents As Variant, ByRef varCalculation As Variant)
  818. varScreenUpdating = Application.ScreenUpdating
  819. varEnableEvents = Application.EnableEvents
  820. varCalculation = Application.Calculation
  821. End Sub
  822.  
  823. Public Sub DisableApplicationSettings()
  824. Application.ScreenUpdating = False
  825. Application.EnableEvents = False
  826. Application.Calculation = xlCalculationManual
  827. End Sub
  828.  
  829. Public Sub RestoreApplicationSettings(ByRef varScreenUpdating As Variant, ByRef varEnableEvents As Variant, ByRef varCalculation As Variant)
  830. Application.ScreenUpdating = varScreenUpdating
  831. Application.EnableEvents = varEnableEvents
  832. Application.Calculation = varCalculation
  833. End Sub
  834.  
  835. Public Sub GetWorkbook(ByVal strFilename As String, ByVal strFilePath As String)
  836.  
  837. Dim bIsWbOpen As Boolean
  838.  
  839. bIsWbOpen = IsWorkbookOpen(strFilename)
  840. If bIsWbOpen = False Then Workbooks.Open strFilePath & strFilename
  841.  
  842. End Sub
  843.  
  844. Public Sub Print2dArrayToSheet(ByRef wbTarget As Workbook, ByRef wsTarget As Worksheet, ByRef arrData As Variant, ByRef rngStartCell As Range)
  845.  
  846. Dim LB1 As Long
  847. Dim UB1 As Long
  848. Dim LB2 As Long
  849. Dim UB2 As Long
  850.  
  851. wbTarget.Activate
  852. wsTarget.Activate
  853.  
  854. Call AssignArrayBounds(arrData, LB1, UB1, LB2, UB2)
  855.  
  856. Dim rngTableRange As Range
  857.  
  858. Set rngTableRange = Range(rngStartCell, Cells(rngStartCell.Row + UB1 - LB1, rngStartCell.Column + UB2 - LB2))
  859.  
  860. rngTableRange = arrData
  861.  
  862. End Sub
  863.  
  864. Public Sub CopyColumn2d(ByRef arrCurrentArray As Variant, ByRef arrDestination As Variant, ByVal lngSourcePosition As Long, ByVal lngDestinationPosition As Long)
  865.  
  866. '/ Copy column from one array to another
  867.  
  868. Dim i As Long
  869.  
  870. For i = LBound(arrCurrentArray) To UBound(arrCurrentArray)
  871. arrDestination(i, lngDestinationPosition) = arrCurrentArray(i, lngSourcePosition)
  872. Next i
  873.  
  874. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement