The txtPeriod, I think refers to successive columns relating to periods(P1,P2,....P12).
First column gives a description of profit and loss items then the successive cols ,the numerical data for each month.
The whole code is as follows:
Public Sub By_Region(ByRef txtPeriod)
i = 2
intRow = 2
Application.ScreenUpdating = False
strPath = Workbooks("Control.xls").Sheets("Control").Range("M2")
strPath2 = Workbooks("Control.xls").Sheets("Control").Range("M3")
'Loops through cells in column A until cell = blank
Do Until Workbooks("Control.xls").Sheets("Control").Range("A" & i) = ""
'assigns a variable to a named range ,this variable
'will refer to the range whenever used in the following code
strWkb = Workbooks("Control.xls").Sheets("Control").Range("J" & intRow)
strFcast = Workbooks("Control.xls").Sheets("Control").Range("D" & i)
o Until Workbooks("Control.xls").Sheets("Control").Range("A" & i) = ""
'assigns a variable to a named range ,this variable
'will refer to the range whenever used in the following code
strWkb = Workbooks("Control.xls").Sheets("Control").Range("J" & intRow)
strFcast = Workbooks("Control.xls").Sheets("Control").Range("D" & i)
'If cell in column C matches the variable assigned above, the
'macro will open a specified workbook and continue to next loop
If Workbooks("Control.xls").Sheets("Control").Range("C" & i) = strWkb Then
Application.StatusBar = "Processing " & strWkb
Workbooks.Open Filename:=strPath & strWkb
Workbooks.Open Filename:=strPath2 & strFcast
Application.DisplayAlerts = False
'The macro will try to find a matching cost centre from
'the trading accounts workbook and copy the period range
'and paste it to the assigned column in forecast model
Do While Workbooks("Control.xls").Sheets("Control").Range("C" & i) = strWkb
strCinema = Workbooks("Control.xls").Sheets("Control").Range("A" & i)
strCinema2 = Workbooks("Control.xls").Sheets("Control").Range("B" & i)
Workbooks(strFcast).Worksheets(strCinema2).Unprotect password:="protect"
Workbooks(strWkb).Sheets("P&L").Activate
Columns("A:A").Select
Selection.Find(What:=strCinema, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
ActiveCell.Offset(0, 0).Range("C3:C61").Copy
'The following code assigns the selected column to
'equal the period entered in the form + 1
'In the forecast models the first column has the
'account names and therefore the first data column
'will be 2
Workbooks(strFcast).Worksheets(strCinema2).Activate
Cells(4, txtPeriod + 1).Activate
ActiveCell.PasteSpecial (xlPasteValues)
Range(Chr(65 + txtPeriod) & "1:" & Chr(65 + txtPeriod) & "80").Select
'sets a color format for the selection
Selection.Interior.ColorIndex = 34
Selection.Locked = True
Cells(65, txtPeriod + 1).Formula = "=" & Chr(65 + txtPeriod) & 9 & "/" & Chr(65 + txtPeriod) & 4
Cells(66, txtPeriod + 1).Formula = "=" & Chr(65 + txtPeriod) & 10 & "/" & Chr(65 + txtPeriod) & 4
Cells(67, txtPeriod + 1).Formula = "=" & Chr(65 + txtPeriod) & 19 & "/" & Chr(65 + txtPeriod) & 9
Cells(68, txtPeriod + 1).Formula = "=" & Chr(65 + txtPeriod) & 20 & "/" & Chr(65 + txtPeriod) & 10
i = i + 1
Loop
intRow = intRow + 1
Workbooks(strWkb).Close
Workbooks(strFcast).Worksheets("Summary by Period").Activate
Range(Chr(65 + txtPeriod) & "1:" & Chr(65 + txtPeriod) & "67").Select
ActiveSheet.Unprotect password:="protect"
Selection.Interior.ColorIndex = 34
ActiveSheet.Protect password:="protect"
Else
i = i + 1
End If
Loop
If strWkb = "" Then
Application.CutCopyMode = False
Application.StatusBar = False
Workbooks("Control.xls").Sheets("Control").Activate
Exit Sub
End If
Application.StatusBar = False
Application.CutCopyMode = False
End Sub
As you can see,
txtPeriod has not been set as a variable or integer, but as an argument by reference(ByRef).Is this sound??
Thanks