Calling a Macro to End a Macro

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I have a few Macros that I would like to make expire monthly to allow for maintenance. I would like to add an Expire Macro and then have this called. That way when I have to update the date I only have to update the Expire Macro and not every Macro that calls it. However I cannot get the Expire Macro to actually stop running the Macro it is called inside (I am not sure how to explain this)

Here is a snip of one of the Macros I want to expire:

VBA Code:
Sub Macro1()
'
' Macro1
Call Expire
Application.ScreenUpdating = False

'Clear all filters and prepare tables for data
Sheets("WO Report").Select
         Dim lo As ListObject
  For Each lo In ActiveSheet.ListObjects
    lo.AutoFilter.ShowAllData
      Next lo
      If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData
End If

Worksheets("WO Report").Rows(2 & ":" & Worksheets("WO Report").Rows.Count).EntireRow.Delete
Range("A2").Select

Sheets("Inv Report").Select

And Here is the Expire Macro:

VBA Code:
Const sPassword = "Password"
 Dim sUserInput
 If Date > CDate("06/09/2024") Then
    sUserInput = InputBox("Enter password to continue...", "Enter Password")
    If Not sUserInput = sPassword Then
        MsgBox "Wrong password, Macro has expired"
        Exit Sub
    End If
 End If
End Sub


Whether a correct password is entered or it is cancelled I want the Expire Macro to end Macro1 as well, but this does not seem to be happening and Macro1 runs anyway even if it is expired/wrong password or no password is entered/cancelled.

I have tried using End instead of Exit Sub but this also does not work.

Help with this would be greatly appreciated
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi, I'd suggest turning your Expire() sub to a function something like this:

VBA Code:
Function Expire() As Boolean
Const sPassword = "Password"
 Dim sUserInput
 If Date > CDate("2024-06-09") Then
    sUserInput = InputBox("Enter password to continue...", "Enter Password")
    If Not sUserInput = sPassword Then
        MsgBox "Wrong password, Macro has expired"
        Expire = True
        Exit Function
    End If
 End If
End Function

Then test what it returns in your Macro1 sub and Exit as appropriate, something like:
VBA Code:
Sub Macro1()

If Expire = True Then Exit Sub
'Rest of code

End Sub
 
Upvote 0
Solution
Hi, I'd suggest turning your Expire() sub to a function something like this:

VBA Code:
Function Expire() As Boolean
Const sPassword = "Password"
 Dim sUserInput
 If Date > CDate("2024-06-09") Then
    sUserInput = InputBox("Enter password to continue...", "Enter Password")
    If Not sUserInput = sPassword Then
        MsgBox "Wrong password, Macro has expired"
        Expire = True
        Exit Function
    End If
 End If
End Function

Then test what it returns in your Macro1 sub and Exit as appropriate, something like:
VBA Code:
Sub Macro1()

If Expire = True Then Exit Sub
'Rest of code

End Sub

This works perfect. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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