How to re-use code in several other sub's

kit99

Active Member
Joined
Mar 17, 2015
Messages
352
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:
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
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Re: Help on how to re-use code in several other sub's

What is ThisGroupPath? You did not declare it, set it, nor pass it as a parameter.
 
Upvote 0
Re: Help on how to re-use code in several other sub's

What is ThisGroupPath? You did not declare it, set it, nor pass it as a parameter.

Thanks for answering my thread.
But after I posted it, I've stumbled upon a solution that involves using "ByVal".
My problem is therefore now solved. :)

This is the solution to my problem:

Code:
Sub UpdateAllGroups()

    Call UpdateGroup("Group1_(M).xlsm", "R2ob - Group1.xls", "R1vo - Group1.xls", "R2vo - Group1.xls")
    Call UpdateGroup("Group2_(M).xlsm", "R2ob - Group2.xls", "R1vo - Group2.xls", "R2vo - Group2.xls")
End Sub

'************************************************************************

Private Sub UpdateGroup(ByVal ThisGroupWb As String, ByVal ReportR2ob As String, ByVal ReportR1vo As String, ByVal ReportR2vo As String)

    'DEFINITIONS

    Dim fPath 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 & "\" & ThisGroupWb)   
        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


    '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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top