I've got 50 wb's Im currently updating using vba. But it's a drag writing (and updating) the same code 50 times, so I'm trying to come up with a method to re-use code. All wb's are equal in structure (sheetnames etc), and the basic "update-vba" should therefore be reusable in all "UpdateGroup-subs". My idea is to place definitions and basic "update-vba" in subs outside the "UpdateGroup-subs", and call upon them when running each "UpdateGroup-sub". But I'm getting a compile error (variable not defined). Is what I'm trying to do even possible? Anyone that can help me out on how to get this to work? I'm uploading 2 versions, one that is working (repeating all code in each "UpdateGroup-sub"), and one that's my attempt at simplify this...
WORKS:
Option Explicit
'************************************************************************
Sub UpdateAllGroups_1_WorksOK()
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer
Call UpdateGroup1
Call UpdateGroup2
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "All Updates is done in " & MinutesElapsed, vbInformation, "Message"
End Sub
'************************************************************************
Private Sub UpdateGroup1()
'DEFINITIONS
Dim fPath, ThisGroupWb, ReportR2ob, ReportR1vo, ReportR2vo As String
Dim WbReport, WbGroup As Workbook
Dim sh_Dash, sh_NewR2ob, sh_NewR1vo, sh_NewR2vo, sh_Time As Worksheet
fPath = ThisWorkbook.Path
If Right(fPath, 1) = "\" Then
fPath = Left(fPath, Len(fPath) - 1)
End If
Set WbGroup = Workbooks.Open(ThisWorkbook.Path & "\Group1_(M).xlsm") 'must be changed in each UpdateGroup-sub
With WbGroup
Set sh_Dash = .Worksheets("Dash")
Set sh_NewR2ob = .Worksheets("NewR2ob")
Set sh_NewR1vo = .Worksheets("NewR1vo")
Set sh_NewR2vo = .Worksheets("NewR2vo")
Set sh_Time = .Worksheets("Time")
End With
ThisGroupWb = "Group1_(M).xlsm" 'must be changed in each UpdateGroup-sub
ReportR2ob = "R2ob - Group1.xls" 'must be changed in each UpdateGroup-sub
ReportR1vo = "R1vo - Group1.xls" 'must be changed in each UpdateGroup-sub
ReportR2vo = "R2vo - Group1.xls" 'must be changed in each UpdateGroup-sub
'NEW REPORTS
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set WbReport = Workbooks.Open(fPath & "\R2ob\" & ReportR2ob)
WbReport.Sheets(1).Cells.Copy sh_NewR2ob.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R1vo\" & ReportR1vo)
WbReport.Sheets(1).Cells.Copy sh_NewR2vo.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R2vo\" & ReportR2vo)
WbReport.Sheets(1).Cells.Copy sh_NewR1vo.Range("A1")
WbReport.Close False
'STORE AND CLOSE GROUP-WB
Application.Goto sh_Dash.Range("A1"), True
WbGroup.Save
WbGroup.Close False
End Sub
'************************************************************************
Private Sub UpdateGroup2()
'DEFINITIONS
Dim fPath, ThisGroupWb, ReportR2ob, ReportR1vo, ReportR2vo As String
Dim WbReport, WbGroup As Workbook
Dim sh_Dash, sh_NewR2ob, sh_NewR1vo, sh_NewR2vo, sh_Time As Worksheet
fPath = ThisWorkbook.Path
If Right(fPath, 1) = "\" Then
fPath = Left(fPath, Len(fPath) - 1)
End If
Set WbGroup = Workbooks.Open(ThisWorkbook.Path & "\Group2_(M).xlsm") 'must be changed in each UpdateGroup-sub
With WbGroup
Set sh_Dash = .Worksheets("Dash")
Set sh_NewR2ob = .Worksheets("NewR2ob")
Set sh_NewR1vo = .Worksheets("NewR1vo")
Set sh_NewR2vo = .Worksheets("NewR2vo")
Set sh_Time = .Worksheets("Time")
End With
ThisGroupWb = "Group2_(M).xlsm" 'must be changed in each UpdateGroup-sub
ReportR2ob = "R2ob - Group2.xls" 'must be changed in each UpdateGroup-sub
ReportR1vo = "R1vo - Group2.xls" 'must be changed in each UpdateGroup-sub
ReportR2vo = "R2vo - Group2.xls" 'must be changed in each UpdateGroup-sub
'NEW REPORTS
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set WbReport = Workbooks.Open(fPath & "\R2ob\" & ReportR2ob)
WbReport.Sheets(1).Cells.Copy sh_NewR2ob.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R1vo\" & ReportR1vo)
WbReport.Sheets(1).Cells.Copy sh_NewR2vo.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R2vo\" & ReportR2vo)
WbReport.Sheets(1).Cells.Copy sh_NewR1vo.Range("A1")
WbReport.Close False
'STORE AND CLOSE GROUP-WB
Application.Goto sh_Dash.Range("A1"), True
WbGroup.Save
WbGroup.Close False
End Sub
'************************************************************************
NOT WORKING:
Option Explicit
'************************************************************************
Sub UpdateAllGroups_2_DoesntWork()
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer
Call UpdateGroup1
Call UpdateGroup2
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "All Updates is done in " & MinutesElapsed, vbInformation, "Message"
End Sub
'************************************************************************
Private Sub Definitions()
Dim fPath, ThisGroupWb, ReportR2ob, ReportR1vo, ReportR2vo As String
Dim WbReport, WbGroup As Workbook
Dim sh_Dash, sh_NewR2ob, sh_NewR1vo, sh_NewR2vo, sh_Time As Worksheet
fPath = ThisWorkbook.Path
If Right(fPath, 1) = "\" Then
fPath = Left(fPath, Len(fPath) - 1)
End If
With WbGroup
Set sh_Dash = .Worksheets("Dash")
Set sh_NewR2ob = .Worksheets("NewR2ob")
Set sh_NewR1vo = .Worksheets("NewR1vo")
Set sh_NewR2vo = .Worksheets("NewR2vo")
Set sh_Time = .Worksheets("Time")
End With
End Sub
'************************************************************************
Private Sub UpdateGroups()
Set WbGroup = Workbooks.Open(ThisWorkbook.Path & "\ThisGroupWb")
'NEW REPORTS
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set WbReport = Workbooks.Open(fPath & "\R2ob\" & ReportR2ob)
WbReport.Sheets(1).Cells.Copy sh_NewR2ob.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R1vo\" & ReportR1vo)
WbReport.Sheets(1).Cells.Copy sh_NewR2vo.Range("A1")
WbReport.Close False
Set WbReport = Workbooks.Open(fPath & "\R2vo\" & ReportR2vo)
WbReport.Sheets(1).Cells.Copy sh_NewR1vo.Range("A1")
WbReport.Close False
'STORE AND CLOSE GROUP-WB
Application.Goto sh_Dash.Range("A1"), True
WbGroup.Save
WbGroup.Close False
End Sub
'************************************************************************
Private Sub UpdateGroup1()
Call Definitions
ThisGroupWb = "Group1_(M).xlsm"
ReportR2ob = "R2ob - Group1.xls"
ReportR1vo = "R1vo - Group1.xls"
ReportR2vo = "R2vo - Group1.xls"
Call UpdateGroups
End Sub
'************************************************************************
Private Sub UpdateGroup2()
Call Definitions
ThisGroupWb = "Group2_(M).xlsm"
ReportR2ob = "R2ob - Group2.xls"
ReportR1vo = "R1vo - Group2.xls"
ReportR2vo = "R2vo - Group2.xls"
Call UpdateGroups
End Sub
Dim fPath, ThisGroupWb, ReportR2ob, ReportR1vo, ReportR2vo As Stringonly declare the last variable as string, all others are variant, for an explanation look here.ThisGroupWb = "Group1_(M).xlsm"in "UpdateGroup1-sub". And if I put this line on "pause", error appear on the next line.