monoj chakraborty
Member
Code:
Sub quartersummary()
'
' quartersummary Macro
' variables
' name of the worksheet, period TY LY, quarters, datarange for ty and ly,shift rows for category, shift cols for country, shift row
'and col for changing quarters
Dim catrow As Integer ' row variable for category
Dim startcol As Integer ' fix start column for this year and last year
Dim shiftcol As Integer 'shift column to change for country
Dim colrange As Integer ' define column for selecting data range
Dim passforQ As Integer 'quarter runs
Dim passforY As Integer 'runs for period
Dim P1, P2 As String 'to set the column to select start and end match periods while offsetting
Dim formulastring As String ' this is the string to define the formula and workaround the character limit
Dim spanstart, spanend As Integer 'to set the span for offsetting
category = Array("insect", "air care", "home cleaning", "auto care", "shoe care") 'array to select category
country = Array("'cambodia'!", "'laos'!", "'myanmar'!", "'brunei'!") 'array to select worksheet for country
fy17 = Array("01jul_fy17", "03sep_fy17", "04oct_fy17", "06dec_fy17", "07jan_fy17", "09mar_fy17", "10apr_fy17", "12jun_fy17") 'array to define the periods to choose from while _
offsetting ty
fy16 = Array("01jul", "03sep", "04oct", "06dec", "07jan", "09mar", "10apr", "12jun") 'array to define the periods to choose from while _
offsetting ly
For passforQ = 3 To 30 Step 9
For passforY = 1 To 2
Select Case passforY
Case Is = 1
startcol = 2
Case Is = 2
startcol = 7
End Select
Select Case passforQ And passforY
Case Is = 3 And 1
P1 = fy17(0)
P2 = fy17(1)
Case Is = 12 And 1
P1 = fy17(2)
P2 = fy17(3)
Case Is = 21 And 1
P1 = fy17(4)
P2 = fy17(5)
Case Is = 30 And 1
P1 = fy17(6)
P2 = fy17(7)
Case Is = 3 And 2
P1 = fy16(0)
P2 = fy16(1)
Case Is = 12 And 2
P1 = fy16(2)
P2 = fy16(3)
Case Is = 21 And 2
P1 = fy16(4)
P2 = fy16(5)
Case Is = 30 And 2
P1 = fy16(6)
P2 = fy16(7)
End Select
For shiftcol = startcol To startcol + 3
Select Case shiftcol
Case Is = startcol
country = country(0)
spanstart = 4
spanend = 119
Case Is = startcol + 1
country = country(1)
spanstart = 4
spanend = 136
Case Is = startcol + 2
country = country(2)
spanstart = 3
spanend = 156
Case Is = startcol + 3
country = country(3)
spanstart = 4
spanend = 262
End Select
For catrow = 3 To 7
formulastring = "=+sum(offset(" & country & "r" & spanstart & "c" & colrange & ":r" & spanend & "c" & colrange + 11 _
& ",0,match(" & P1 & "'" & country & "r" & spanstart - 1 & "c" & colrange & ":r" & spanstart - 1 & "c" & colrange + 11 & ",0)-1" _
& "," & (spanend - spanstart + 1) & ",(match(" & P2 & "," & country & _
"r" & spanstart - 1 & "c" & colrange & ":r" & spanstart - 1 & "c" & colrange + 11 & ",0)-match(" & _
P1 & "," & country & "r" & spanstart - 1 & "c" & colrange & ":r" & spanstart - 1 & "c" & colrange + 11 & ",0)+1))*(" _
& country & "r" & spanstart & "c3:r" & spanend & "c3=""" & category(catrow - 3) & """))/1000/36.11)"
With Sheets("sheet19").Range(catrow, startcol).Address ' this is the part where it shows the run time error..."subscript of out range"
.FormulaArray = formulastring
End With
Next catrow
Next shiftcol
Next passforY
Next passforQ
End Sub
Last edited by a moderator: