I have a Control code which calls both of these modules, however I need to keep wb2 as a particular workbook.
Can anyone please provide the correct code (syntax) as to how I pass the wb2 from my code into the next module?
The workbook names will change every week hence the need for a flexible code.
as always, thanks in advance.
Stuart
Can anyone please provide the correct code (syntax) as to how I pass the wb2 from my code into the next module?
The workbook names will change every week hence the need for a flexible code.
Code:
Sub OPEN_BOTH_FILES()
Dim wb1 As Workbook, wb2 As Workbook, wb3 As Workbook
Dim Ret2, Ret3
Dim LastRow As Long
Dim LastRowX As Long
Dim LastRowY As Long
Dim LastRowZ As Long
Dim FileName As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False
Application.CutCopyMode = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Set wb1 = ThisWorkbook
'**************************************************************************************
'OPEN THE MI DATA FILE wb2,Ret2
'**************************************************************************************
ChDir "\\Intranet.barcapint.com\dfs-emea\BWGroup\BWData\Kyc\FATCA\BAU\MI\"
Ret2 = Application.GetOpenFilename("Excel Files (*.xlsx*), *.xlsx*", _
, "Please select file")
If Ret2 = False Then End
Dim strPath As String
strPath2 = Ret2
Dim strFile As String
strFile = Right(strPath2, Len(strPath2) - InStrRev(strPath2, "\"))
FileName = CreateObject("Scripting.FileSystemObject").GetBaseName(strFile)
Set wb2 = Workbooks.Open(Ret2, Local:=True)
MsgBox "Now Open Source File"
'**************************************************************************************
'OPEN THE SOURCE DATA FILE wb3,Ret3
'**************************************************************************************
ChDir "\\Intranet.barcapint.com\dfs-emea\BWGroup\BWData\Kyc\FATCA\BAU\MI\"
Ret3 = Application.GetOpenFilename("Excel Files (*.csv*), *.csv*", _
, "Please select file")
If Ret3 = False Then End
Set wb3 = Workbooks.Open(Ret3, Local:=True, ReadOnly:=True)
DataFile = FileName & ".xslx"
With wb3
wsName = wb1.Sheets("Previous MI").Range("A188")
ws1name = wsName & " Full extract"
End With
Set DataFile = wb2
With DataFile
LastRowX = DataFile.Sheets("Extract").Range("A" & _
DataFile.Sheets("Extract").Rows.Count).End(xlUp).Row
LastRowY = DataFile.Sheets("Post-May").Range("A" & _
DataFile.Sheets("Post-May").Rows.Count).End(xlUp).Row
DataFile.Sheets("Post-May").Range("A2:BU" & LastRowX).ClearContents
DataFile.Sheets("Extract").Range("A2:BU" & LastRowX).ClearContents
LastRowZ = wb3.Sheets(ws1name).Range("A" & _
wb3.Sheets(ws1name).Rows.Count).End(xlUp).Row
wb3.Sheets(ws1name).Range("A1:BU" & LastRowZ).Copy Destination:=DataFile.Sheets("Extract").Range("A1")
End With
wb3.Close SaveChanges:=False
'************************************************************************
' Extract Sheet - Add Formula
'************************************************************************
With DataFile
wb2.Sheets("Extract").Range("BT1").Value = "Reportable?"
wb2.Sheets("Extract").Range("BU1").Value = "Source System"
wb2.Sheets("Extract").Range("BT2:BT" & LastRowZ).Formula = "=IF(ISBLANK(BH2),""No Status"",VLOOKUP(BH2,Reportable!$1:$52,2,0))"
wb2.Sheets("Extract").Range("BU2:BU" & LastRowZ).Formula = "=IF(MID(B2,FIND("":"",B2)+1,3)=""T12"",""ADIR"",IF(MID(B2,FIND("":"",B2)+1,3)=""T13"",""TS2"",IF(MID(B2,FIND("":"",B2)+1,3)=""59-"",""Wealthmaster"",IF(MID(B2,FIND("":"",B2)+1,3)=""46-"",""WECCO or Scars"",IF(MID(B2,FIND("":"",B2)+1,3)=""26-"",""Avaloq"",IF(MID(B2,FIND("":"",B2)+1,3)=""40-"",""OSCARS"",IF(MID(B2,FIND("":"",B2)+1,3)=""43-"",""Sharedeal"",IF(MID(B2,FIND("":"",B2)+1,3)=""65-"",""Paladign"",IF(MID(B2,FIND("":"",B2)+1,1)=""T"",""ADIR"",0)))))))))"
wb2.Sheets("Extract").Range("BT2:BU" & LastRowZ).Copy
wb2.Sheets("Extract").Range("BT2:BU" & LastRowZ).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End With
With wb1
wb1.Sheets("Previous MI").Range("A220").Value = strFile
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
ActiveSheet.DisplayPageBreaks = True
Application.CutCopyMode = True
Application.EnableEvents = True
End Sub
Code:
Sub AdjustPtSourceA()
Dim PT As PivotTable
'This is where wb2 is required
With wb2
Sheets("Post-May").AutoFilterMode = False
Set PT = Sheets("Pivot Post-May").PivotTables("PivotTable1")
PT.PivotCache.SourceData = Sheets("Post-May").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
End With
With wb2
Set PT2 = Sheets("Pivot Post-May").PivotTables("PivotTable2")
PT.PivotCache.SourceData = Sheets("Post-May").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
End With
With wb2
Set PT = Sheets("Extract").PivotTables("PivotTable1")
PT.PivotCache.SourceData = Sheets("Pivot").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
End With
End Sub
as always, thanks in advance.
Stuart