+ Reply to Thread
Results 1 to 6 of 6

Allow run macro only within current month

Hybrid View

fabian_76 Allow run macro only within... 10-29-2018, 11:03 AM
Logit Re: Allow run macro only... 10-29-2018, 12:04 PM
fabian_76 Re: Allow run macro only... 10-29-2018, 12:12 PM
6StringJazzer Re: Allow run macro only... 10-29-2018, 12:08 PM
fabian_76 Re: Allow run macro only... 10-29-2018, 12:14 PM
Logit Re: Allow run macro only... 10-29-2018, 01:07 PM
  1. #1
    Forum Contributor
    Join Date
    03-14-2015
    Location
    Mexico
    MS-Off Ver
    365
    Posts
    285

    Allow run macro only within current month

    Hi people,

    I was wonder if there is a code that prohibit the use of the macro out of the period declared. For example I want that users can run the macro only in November from 1rst - 30th. If they want to use the macro in December, they would need to come to me so I can change the code for a new period and so forth.

    I appreciate it.

    Thanks,

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,404

    Re: Allow run macro only within current month

    .
    Option Explicit
    
    Sub rundates()
    
        Dim early As Date, late As Date
        Dim dt As Date
       
        early = "10/25/2018"
    
        late = "10/30/2018"
    
            dt = Now()
            If dt <= late And dt >= early Then
    
                MsgBox "Hello" '<-- replace this with your macro code or a call to your macro code located elsewhere.
    
            End If
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    03-14-2015
    Location
    Mexico
    MS-Off Ver
    365
    Posts
    285

    Re: Allow run macro only within current month

    Thank you Logit, this worked great!

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,787

    Re: Allow run macro only within current month

    How do you specify what the "period declared" is? In the code? Here is an example that will allow the code to run only in November:

    Public Sub Example()
    
       Const AllowedMonth = 11
    
       If Month(Date) <> AllowedMonth Then
          MsgBox "You may only run this code in " & Format(DateSerial(Year(Date), AllowedMonth, 1), "Mmmm")
          Exit Sub
       End If
       
       ' the rest of your code goes here
       
    End Sub
    HOWEVER, the fact that you said that you have to update code for a new period suggests that you have a design problem. Your code should be written to be valid for whatever period your data indicates.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Forum Contributor
    Join Date
    03-14-2015
    Location
    Mexico
    MS-Off Ver
    365
    Posts
    285

    Re: Allow run macro only within current month

    Thank you, I can certainly have design problems. I am new on this, thank you for letting me know this, otherwise I would keep doing the same. btw your code worked great too.

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,404

    Re: Allow run macro only within current month

    .
    You are welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2016, 01:28 PM
  2. Macro for Previous and Current Month
    By angeleenmc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2015, 12:56 PM
  3. [SOLVED] Macro to cut and paste current month sheet into the relevant month chosen by a drop down
    By scotty22 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-06-2015, 09:07 AM
  4. [SOLVED] Set PivotTable Filter to Current Day, Current Week, Current Month, or Current Year
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2014, 08:31 AM
  5. [SOLVED] Pick a cell containing current month actulas based what the current month is.
    By vanbasten007 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-10-2014, 01:17 AM
  6. [SOLVED] Macro to find current month in row
    By Stratfordoaks in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2012, 03:02 PM
  7. Macro using vlookups comparing 2 months paysheet(previous month and current month)
    By srinivasan1965 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-07-2012, 03:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1