Sub CashProjectionData()
Dim ar As Variant ' Column Number
Dim var As Variant ' Sheet Name
Dim i As Integer
Dim j As Integer
Dim x As Long
Dim wb As Workbook
Dim wsCash As Worksheet
Dim wsData As Worksheet
Dim wsFX As Worksheet
Dim ws As Worksheet
Dim lw As Long
Dim lRow As Long
Dim LGIMPass As Variant
Dim lr As Long
Dim sh As Worksheet
Dim ThisFile As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Open Template where all the data is imported
Set sh = Sheet2 ' Macro worksheet
Set wb = Workbooks.Open(sh.[E11].Value) ' Daily Cash Forecasting workbook
Set wsCash = Worksheets(sh.[C11].Value) ' Cash sheet on Template
Set wsData = Worksheets(sh.[C12].Value) ' Data sheet on Template
Set wsFX = Worksheets(sh.[C13].Value) ' FX Rates sheet on Template
ar = sh.Range("F2", sh.Range("M" & Rows.Count).End(xlUp)) 'Column data is picked up from F2 to M
var = sh.Range("C2", sh.Range("C" & Rows.Count).End(xlUp))
LGIMPass = sh.[A3].Value 'Password for Portected file
'Copy data for a given columns only.
For j = 2 To 3
Set wb = Workbooks.Open(sh.Range("E" & j)) 'File Path & Name
lw = Sheets(var(j - 1, 1)).Range("A" & Rows.Count).End(xlUp).Row
Sheets(var(j - 1, 1)).Range("A6:J" & lw).Copy wsCash.Range("A" & Rows.Count).End(xlUp).Offset(1) 'Copy A6:J as long as there is data
If wsCash.Range("A" & Rows.Count).End(xlUp) = "P 12876" Then 'The account number
wsCash.Range("C" & Rows.Count).End(xlUp) = "LQD FUNDS" 'Paste value
End If
wb.Close 'Close the workbook
Next j
'Copy data for a given columns only.
For j = 4 To 5
Set wb = Workbooks.Open(sh.Range("E" & j), Password:=LGIMPass) 'File Path & Name
lw = Sheets(var(j - 1, 1)).Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To 4 'Loop through the Array
Sheets(var(j - 1, 1)).Range(Cells(2, ar(j - 1, i)), Sheets(var(j - 1, 1)).Cells(lw, ar(j - 1, i))).Copy wsCash.Cells(Rows.Count, ar(j - 1, i + 4)).End(xlUp).Offset(1, 0)
Next i
wb.Close 'Close the workbook
Next j
'Copy data for a given columns only.
j = 6
Set wb = Workbooks.Open(sh.Range("E" & j)) 'File Path & Name
Sheets(var(j - 1, 1)).Activate
lw = Sheets(var(j - 1, 1)).Range("B" & Rows.Count).End(xlUp).Row
For i = 1 To 4 'Loop through the Array
Sheets(var(j - 1, 1)).Range(Cells(6, ar(j - 1, i)), Sheets(var(j - 1, 1)).Cells(lw, ar(j - 1, i))).Copy wsCash.Cells(Rows.Count, ar(j - 1, i + 4)).End(xlUp).Offset(1, 0)
Next i
wb.Close 'Close the workbook
'Fill in formula to get account number from text in column B
lr = wsCash.Range("B" & Rows.Count).End(xlUp).Row
wsCash.Range("A2:A" & lr).SpecialCells(4) = "=IFERROR(IF(LEFT(MID(RC2,FIND("" "",RC2,1)-5,5),1)="" "",0&MID(RC2,FIND("" "",RC2,1)-4,4),MID(RC2,FIND("" "",RC2,1)-5,5)),MID(RC[1],25,5))"
'Fill LQD FUNDS in column C where appropriate
wsCash.Range("C2:C" & lr).SpecialCells(4) = "LQD FUNDS"
'Copy data from...
j = 7
Set wb = Workbooks.Open(sh.Range("E" & j))
lRow = Sheets(var(j - 1, 1)).Range("B65535").End(xlUp).Row
For x = 2 To lRow
If Sheets(var(j - 1, 1)).Range("I" & x).Value = "Net Projected Balance (GBP)" Then 'Copy data if Column I is equal to "Net Projected Balance (GBP)"
wsCash.Range("A" & Rows.Count).End(xlUp).Offset(1) = "301371" 'Column A will have value 301371
wsCash.Range("I" & Rows.Count).End(xlUp).Offset(1) = Sheets(var(j - 1, 1)).Range("C" & x).Value 'Column I will have value from Column C Data file
wsCash.Range("J" & Rows.Count).End(xlUp).Offset(1) = Sheets(var(j - 1, 1)).Range("K" & x).Value 'Column I will have value from Column K Data file
Sheets(var(j - 1, 1)).Range("E" & x & ":H" & x).Copy
wsData.Range("D" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Transpose:=True 'Copy Columns E to H and transpose and paste on Data tab
wsData.Range("D" & Rows.Count).End(xlUp).Offset(1).FormulaR1C1 = "=WORKDAY(R[-1]C,1)" 'Next working day date in next cell
Sheets(var(j - 1, 1)).Range("L" & x & ":P" & x).Copy
wsData.Range("J" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Transpose:=True 'Copy Columns L to P and transpose and paste on Data tab
End If
Next x
wb.Close 'Close the workbook
lr = wsData.Range("J" & Rows.Count).End(xlUp).Row
wsData.Range("A2:A" & lr).SpecialCells(4) = "301371" 'Fill column A with 301371
wsData.Range("I2:I" & lr).SpecialCells(4) = "GBP" 'Fill column A with GBP
lr = wsCash.Range("A" & Rows.Count).End(xlUp).Row
wsCash.Range("D2:D" & lr) = wsCash.[E2].Value 'Column D populated with same data as Column E
wsCash.Range("K2:K" & lr) = "=VLOOKUP(I2,'FX Rates'!B:C,2,0)*J2" 'V-lookup to get the exchange rate from Sheet Named FX Rates
wsCash.Range("L2:L" & lr) = "=IF(ISERROR(LOOKUP(2,1/($P$2:$P$80=A2)/($Q$2:$Q$80=I2),($R$2:$R$80))),VLOOKUP(A2,$P$2:$R$80,3,FALSE),(LOOKUP(2,1/($P$2:$P$80=A2)/($Q$2:$Q$80=I2),($R$2:$R$80))))" 'V-lookup to get the FM name from Columns P,Q and R
wsCash.Range("A2:J" & lr).Copy wsData.Range("A" & Rows.Count).End(xlUp).Offset(1) 'Copy data from Template "Cash" tab to "Data" tab
'Copy data from Cash Projection.
j = 8
Set wb = Workbooks.Open(sh.Range("E" & j)) 'File Path & Name
lw = Sheets(var(j - 1, 1)).Range("A" & Rows.Count).End(xlUp).Row
Sheets(var(j - 1, 1)).Range("A7:J" & lw).Copy wsData.Range("A" & Rows.Count).End(xlUp).Offset(1) 'Copy A6:J as long as there is data
wb.Close 'Close the workbook
'Copy data from FX rates.
j = 9
Set wb = Workbooks.Open(sh.Range("E" & j)) 'File Path & Name
lw = Sheets(var(j - 1, 1)).Range("A" & Rows.Count).End(xlUp).Row
Sheets(var(j - 1, 1)).Range("A6:D" & lw).Copy wsFX.Range("A" & Rows.Count).End(xlUp).Offset(1) 'Copy A6:J as long as there is data
wb.Close 'Close the workbook
lr = wsData.Range("A" & Rows.Count).End(xlUp).Row
wsData.Range("K2:K" & lr) = "=VLOOKUP(I2,'FX Rates'!B:C,2,0)*J2" 'V-lookup to get the exchange rate from Sheet Named FX Rates
wsData.Range("L2:L" & lr) = "=IF(ISERROR(LOOKUP(2,1/($P$2:$P$80=A2)/($Q$2:$Q$80=I2),($R$2:$R$80))),VLOOKUP(A2,$P$2:$R$80,3,FALSE),(LOOKUP(2,1/($P$2:$P$80=A2)/($Q$2:$Q$80=I2),($R$2:$R$80))))" 'V-lookup to get the FM name from Columns P,Q and R
'Clear cells depending on the account code from Data tab
With wsData.UsedRange
.AutoFilter Field:=1, Criteria1:="45365", Operator:=xlOr, Criteria2:="74564"
'.AutoFilter Field:=1, Criteria1:="74564"
.Range("A2" & ":L10000").ClearContents
.AutoFilter
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
'Save Template as a dated file in Cash Projection data folder
ThisFile = sh.[E10].Value
Workbooks("Daily Cash Forecasting").SaveAs Filename:=ThisFile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub