Sub ActivitiesForecastsExtract(iOption As Integer)
'This is the Direct Activities, Enhancements, Indirect Activities, Overheads or Projects routine
'
'Input option iOption has the following possible enumerated values:
' a. nUseAllDIR - process for Direct Activities
' b. nUseAllEH - process for Enhancements
' c. nUseAllIND - process for Indirect Activities
' d. nUseAllOVH - process for Overheads
' e. nUseAllPRO - process for Projects
Dim a
Dim ad As Worksheet
Dim bottomB As Integer
Dim dic As Object
Dim i As Long
Dim Mmonth
Dim rng As Range
Dim ws As Worksheet
Dim Y()
'Define a boolean Value which is 'True' if Column 'C' Consultancy and Requirements' are met AND 'False' otherwise
Dim bColumnCIsConsultancyAndRequirements As Boolean
Application.ScreenUpdating = False
Set ad = Sheets("All Data")
bottomB = Range("B" & Rows.Count).End(xlUp).Row
For Each rng In ad.Range("B8:B" & bottomB)
If rng > 0 Then
Set ws = Sheets(rng.Value)
Application.ScreenUpdating = False
With Worksheets("All Data")
a = .Range("B8").CurrentRegion ' Load the required range in to array, named "a"
End With
Set dic = CreateObject("scripting.dictionary")
dic.comparemode = 1
With ws
With dic
For i = 2 To UBound(a) ' Loop through rows
'test if column C is "Consultancy & Requirements"
'Initialize to NOT 'Consultancy and Requirements'
bColumnCIsConsultancyAndRequirements = False
'Test either Direct Activities, Enhancements, Indirect Activities, Overheads or Projects for Column 'C' 'Consultancy and Requirements' conditions
Select Case iOption
'Direct Activities processing test for Column 'C' 'Consultancy and Requirements' conditions
Case nUseAllDIR
If a(i, 1) = ws.Name And InStr(a(i, 6), "Consultancy & Innovation") = 0 And _
InStr(a(i, 8), "TM - DIR") > 0 Then
'Set the Column 'C' is 'Consultancy & Requirements' boolean value to true
'Conditions have been met for Direct Activities
bColumnCIsConsultancyAndRequirements = True
End If
'Enhancements processing test for Column 'C' 'Consultancy and Requirements' conditions
Case nUseAllEH
If a(i, 1) = ws.Name And InStr(a(i, 6), "Consultancy & Innovation") = 0 And _
InStr(a(i, 8), "Enhancements") > 0 Then
'Set the Column 'C' is 'Consultancy & Requirements' boolean value to true
'Conditions have been met for Enhancements
bColumnCIsConsultancyAndRequirements = True
End If
'Indirect Activities processing test for Column 'C' 'Consultancy and Requirements' conditions
Case nUseAllIND
If a(i, 1) = ws.Name And InStr(a(i, 6), "Consultancy & Innovation") = 0 And _
InStr(a(i, 8), "TM - IND") > 0 Then
'Set the Column 'C' is 'Consultancy & Requirements' boolean value to true
'Conditions have been met for Indirect Activities
bColumnCIsConsultancyAndRequirements = True
End If
'Overheads processing test for Column 'C' 'Consultancy and Requirements' conditions
Case nUseAllOVH
If a(i, 1) = ws.Name And InStr(a(i, 6), "Consultancy & Innovation") = 0 And _
InStr(a(i, 8), "TM - OVH") > 0 Then
'Set the Column 'C' is 'Consultancy & Requirements' boolean value to true
'Conditions have been met for Indirect Activities
bColumnCIsConsultancyAndRequirements = True
End If
'Projects processing test for Column 'C' 'Consultancy and Requirements' conditions
Case nUseAllPRO
If a(i, 1) = ws.Name And InStr(a(i, 6), "Consultancy & Innovation") = 0 And _
InStr(a(i, 8), "TM - ") + _
InStr(a(i, 8), "Enhancements") = 0 Then
'Set the Column 'C' is 'Consultancy & Requirements' boolean value to true
'Conditions have been met for Indirect Activities
bColumnCIsConsultancyAndRequirements = True
End If
End Select
If bColumnCIsConsultancyAndRequirements = True Then 'test if column C is "Consultancy & Requirements"
Mmonth = Trim(Format(a(i, 12), "mmm yy")) ' format the date in to mmm-yy
If Not .exists(Mmonth) Then 'If column C cells do not exist, load column E value in to dictionary using column C as item
.Item(Mmonth) = a(i, 14)
Else
.Item(Mmonth) = .Item(Mmonth) + a(i, 14) 'If column C cells do exist, then add column E in to existing item
End If
End If
Next
End With
With ws
a = .Range("C7", .Cells(7, .Columns.Count).End(xlToLeft)) ' Load the required range in to array, named "a"
End With
ReDim Y(1 To 2, 1 To UBound(a, 2))
With dic
For i = 1 To UBound(a, 2) ' Loop through rows
Mmonth = Trim(Format(a(1, i), "mmm yy")) ' format the date in to mmm-yy
If .exists(Mmonth) Then 'If column C cells do exist then copy the the dictionary in to match column
Y(1, i) = .Item(Mmonth)
End If
Next
End With
With ws
'Process either Direct Activities, Enhancements, Indirect Activities, Overheads or PROJECTS for resize
Select Case iOption
'Direct Activities processing resize
Case nUseAllDIR
.Range("C9").Resize(1, i - 1) = Y() 'Result-load Y in to C8
'Enhancements processing resize
Case nUseAllEH
.Range("C10").Resize(1, i - 1) = Y() 'Result-load Y in to C8
'Indirect Activities processing resize
Case nUseAllIND
.Range("C11").Resize(1, i - 1) = Y() 'Result-load Y in to C8
'Overheads processing resize
Case nUseAllOVH
.Range("C12").Resize(1, i - 1) = Y() 'Result-load Y in to C8
'Projects processing resize
Case nUseAllPRO
.Range("C13").Resize(1, i - 1) = Y() 'Result-load Y in to C
End Select
End With
End With
End If
Next rng
Set dic = Nothing ' clear dic
End Sub
The code initially selects column