Question : Using Modules in VBA to prevent repeating codes in sheets

In my workbook I am trying to implement an unlock procedure to try and encourage people to report problems they are having with thte workbooks.

On each page I want to be able to click a button to run the attached code.

I have the button, I have the code and I can make it work by putting this code on every sheet with the individual buttons but this seems stupid, why right the code on every sheet when you can right it once and have each sheet link to it?

Thanks for the help,

Haydan


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
Public Sub SheetUnlocker()

Application.DisplayAlerts = False

'=================
'Unlocks the Sheet
'=================

ActiveSheet.Unprotect ("BlueberryPies")

'================================
'Creates the email for the unlock
'================================

'From Ron Bruin
'Working in 2000-2007
'This example send the last saved version of the Activeworkbook
    Dim OutApp As Object
    Dim OutMail As Object
    Dim PDFUpdate As String
    Dim strbody As String
    Dim SigString As String
    Dim Signature As String
    
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "[email protected]"
        .CC = ""
        .BCC = ""
        .Subject = "Unlock Event - " & ActiveSheet.Name
        .HTMLBody = "Please right a brief description of why the unlock was needed"
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send or Display
    End With
    
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing

'=======================
'Ends creating the email
'=======================

Application.DisplayAlerts = True

End Sub

Answer : Using Modules in VBA to prevent repeating codes in sheets

2000/3....
Workaround is to move the code to a Module, and have the command buttons call it.

Private Sub CommandButton1_Click()
  SheetUnlocker    ' this lives in a module
End Sub
Random Solutions  
 
programming4us programming4us