I've got around 50 wb's I'm currently updating using vba. But It'a a drag writing (and updating) the same code 50 times, so I'm trying to come up with a way to re-use code.
The code below got a lot of unnecessary code (Activate, Select), but I can change that later. What's important is if what I'm trying to do is possible? My code is not running. It stops with a run-time error on this line:
Here is my code:
The code below got a lot of unnecessary code (Activate, Select), but I can change that later. What's important is if what I'm trying to do is possible? My code is not running. It stops with a run-time error on this line:
Code:
'Open up ThisGroup-wb
Set wb = Workbooks.Open(ThisWorkbook.Path & ThisGroupPath)
Here is my code:
Code:
Sub UpdateAllGroups()
Call UpdateGroup1
Call UpdateGroup2
End Sub
Private Sub Definitions()
'This sub is ment to be re-used for x-number of groups...
fPath = ThisWorkbook.Path
If Right(fPath, 1) <> "\" Then fPath = fPath
End Sub
Private Sub UpdateGroups()
'This sub is ment to be re-used for x-number of groups...
'Open up ThisGroup-wb
Set wb = Workbooks.Open(ThisWorkbook.Path & ThisGroupPath)
'Open up report-wb
Set wb = Workbooks.Open(fPath & "\R2ob\" & reportR2ob)
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
'Copy report-wb into ThisGroup-wb
Windows(ThisGroupPath).Activate
Sheets("NewR2ob").Select
Cells.Select
ActiveSheet.Paste
'Next report
Set wb = Workbooks.Open(fPath & "\R1vo\" & reportR1vo)
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
Windows(ThisGroupPath).Activate
Sheets("NewR1vo").Select
Cells.Select
ActiveSheet.Paste
'Next report
Set wb = Workbooks.Open(fPath & "\R2vo\" & reportR2vo)
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
Windows(ThisGroupPath).Activate
Sheets("NewR2vo").Select
Cells.Select
ActiveSheet.Paste
End Sub
Private Sub UpdateGroup1()
Call Definitions
ThisGroupPath = "Group1_(M).xlsm"
reportR2ob = "R2ob - Group1.xls"
reportR1vo = "R1vo - Group1.xls"
reportR2vo = "R2vo - Group1.xls"
Call UpdateGroups
End Sub
Private Sub UpdateGroup2()
Call Definitions
ThisGroupPath = "Group2_(M).xlsm"
reportR2ob = "R2ob - Group2.xls"
reportR1vo = "R1vo - Group2.xls"
reportR2vo = "R2vo - Group2.xls"
Call UpdateGroups
End Sub