Call macro not working

cdwr0550

New Member
Joined
Aug 30, 2017
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have a macro that is set to save and close the a workbook after a set time. I also have a macro that protects all sheets within the workbook. My problem is when I try and call ProtectAllWorksheets macro I get an error sub or function not defined. I know the macro works. I have tried moving the ProtectAllWorksheets to the module and that made the macro work. However, it also locked all cells in every open workbook.

Code below is in a Module:
VBA Code:
Public NoActivity As Date
Sub StopClock()

On Error Resume Next

Application.OnTime NoActivity, "ShutDown", , False

End Sub
Sub StartClock()

NoActivity = now + TimeValue("00:02:00")

Application.OnTime NoActivity, "ShutDown"

End Sub



Sub ADD()
AddNew.show
End Sub


Sub ShutDown()
Call ProtectAllWorksheets


Application.DisplayAlerts = False

With ThisWorkbook



'.Save
'.Close
End With

End Sub

Code below is in ThisWorkBook
Code:
Private Sub Workbook_Open()

MsgBox ("The data entry log is set to save and close in 2 minutues of inactivity")


Call StartClock
End Sub



Private Sub Workbook_SheetCalculate(ByVal sh As Object)

Call StopClock

Call StartClock

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Call StopClock

Call ProtectAllWorksheets
    
    
    
    ActiveWorkbook.Save

    
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)

Call StopClock

Call StartClock

End Sub
Sub ProtectAllWorksheets()
 Dim Ws As Worksheet
   For Each Ws In Worksheets
         Ws.Unprotect "clcRox"
        

  
      Ws.Cells.Locked = True
      Ws.protect "clcRox", AllowFiltering = True
   Next Ws
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Two things you can try:
- compile your code project and see what happens.
- omit the Call keyword; just use

Sub ShutDown()
ProtectAllWorksheets
etc.
Often the error message is the result of missing references but not in the case of UDF's I think. If your vba project (the actual compiled code) is out of sync with what you read in the editor you can get strange errors happening yet everything looks correct.
You sure have a lot of empty space between your code lines :unsure:
 
Upvote 0
I have tried moving the ProtectAllWorksheets to the module and that made the macro work. However, it also locked all cells in every open workbook.
Yes, a code module is the correct place for ProtectAllWorksheets.

I'm not sure what you mean by every open workbook? As written, the Sub locks the cells For Each Ws In Worksheets, i.e. For Each Ws In ActiveWorkbook.Worksheets

Try changing to For Each Ws In ThisWorkbook.Worksheets
 
Upvote 0
Solution

Forum statistics

Threads
1,215,873
Messages
6,127,470
Members
449,384
Latest member
purevega

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