+ Reply to Thread
Results 1 to 2 of 2

How to add macro

  1. #1
    Registered User
    Join Date
    11-18-2011
    Location
    Slovakia
    MS-Off Ver
    Excel 2007
    Posts
    6

    How to add macro

    Hello, I have a code in workbok for auto closing of the workbook after certain time of inactivity.
    But, before autoclosing Id like to add one macro named for instance "macro1" / of course macro1 is working fine, but only manually/
    thanx a lot for any ideas !!


    In a general module:
    Code:

    Dim DownTime As Date

    Sub SetTime()
    DownTime = Now + TimeValue("00:00:20")
    Application.OnTime DownTime, "ShutDown"
    End Sub

    Sub ShutDown()
    ThisWorkbook.Save
    ThisWorkbook.Close
    End Sub

    Sub Disable()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=False
    End Sub


    In the ThisWorkbook object:
    Code:

    Private Sub Workbook_Open()
    MsgBox "This workbook will auto-close after 20 seconds of inactivity"
    Call SetTime
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call Disable
    End Sub

    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call Disable
    Call SetTime
    End Sub

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    Call Disable
    Call SetTime
    End Sub

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: How to add macro

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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