Combine multiple simple modules into 1

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, I have created 3 Macros that I have combined into a macro button - but I wanted to see if there is a way to "combine them" into one sub and make things more efficient, keeping the same order. Unfortunately, I am somewhat of a novice so I am not sure how to go about this. Any assistance would be much appreciated.

Logic:

  1. Initiate msg box. If no, exit. If yes, delete all sheets beginning with "Labor BOE"
  2. Copy template multiple times, name/number them sequentially (Labor BOE 1 of 2, Labor BOE 2 of 2...()
  3. Copy paste value in cell A1, then display Msg Box "BOE Generation Complete"


Macro Button:

Code:
Sub GenerateBOEs_Button()
    GenerateBOEs_1
    GenerateBOEs_2
    GenerateBOEs_3
End Sub

MACRO 1:
Code:
Sub GenerateBOEs_1()
If MsgBox("Do you want to generate BOEs?" & Chr(10) & Chr(10) & "Warning:  All existing BOEs will be deleted!  This action cannot be undone!", vbYesNo, "Confirm") = vbYes Then
Application.DisplayAlerts = False
On Error Resume Next
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    If Left(sh.name, 9) = "Labor BOE" Then
        sh.Delete
    End If
    
Next sh
Application.DisplayAlerts = True
On Error GoTo 0
End If
End Sub


MACRO 2:
Code:
Sub GenerateBOEs_2()
Dim i As Integer
Dim i2 As Integer
Dim sh As Worksheet
' Set the worksheet to copy
Set sh = Worksheets("Template")
' Get the number of sheets to copy
i = Worksheets("Staffing Plan").Range("C5")
' Say how you want to name the first sheet
sh.name = "Template"
i2 = ActiveWorkbook.Worksheets.Count
For X = 1 To i
    sh.Copy After:=Sheets(i2 + X - 1)
    ' Say how you want to name subsequent sheets
    Sheets(X + i2).name = "Labor BOE " & X & " of " & i
Next X
End Sub


MACRO 3:
Code:
Sub GenerateBOEs_3()
    
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
    If Left(sh.name, 9) = "Labor BOE" Then
        sh.Select
        With sh.Range("A1")
            .Value = .Value
        End With
    End If
        Application.CutCopyMode = False
    Next sh
MsgBox "BOE Generation Complete"
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Here is what I did, but the issue is that if I select "No" in the message box it still carries out the remaining steps.

I am also curious if this is "efficient" / "good code" or if you have any suggestions.

Code:
Sub GenerateBOEs_1()
If MsgBox("Do you want to generate BOEs?" & Chr(10) & Chr(10) & "Warning:  All existing BOEs will be deleted!  This action cannot be undone!", vbYesNo, "Confirm") = vbYes Then
Application.DisplayAlerts = False
On Error Resume Next
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    If Left(sh.name, 9) = "Labor BOE" Then
        sh.Delete
    End If
    
Next sh
Application.DisplayAlerts = True
On Error GoTo 0
End If

Dim i As Integer
Dim i2 As Integer
' Set the worksheet to copy
Set sh = Worksheets("Template")
' Get the number of sheets to copy
i = Worksheets("Staffing Plan").Range("C5")
' Say how you want to name the first sheet
sh.name = "Template"
i2 = ActiveWorkbook.Worksheets.Count
For X = 1 To i
    sh.Copy After:=Sheets(i2 + X - 1)
    ' Say how you want to name subsequent sheets
    Sheets(X + i2).name = "Labor BOE " & X & " of " & i
Next X
For Each sh In ActiveWorkbook.Worksheets
    If Left(sh.name, 9) = "Labor BOE" Then
        sh.Select
        With sh.Range("A1")
            .Value = .Value
        End With
    End If
        Application.CutCopyMode = False
    Next sh
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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