+ Reply to Thread
Results 1 to 8 of 8

disabling macros when closing WB

Hybrid View

  1. #1
    Registered User
    Join Date
    01-01-2010
    Location
    Florida
    MS-Off Ver
    Excel 2003, 2007
    Posts
    5

    Exclamation disabling macros when closing WB

    I am working on a small project that is complete but when i close the workbook and open a new workbook, my paste special/values macro that overrides the paste in the right click menu is still active. How do I disable all macros automatically upon closing workbook or how to disable specific macros auto.
    here is the code for the paste special/values used on auto open and also a module.

    Sub Auto_Open()
    
    
        Application.CommandBars("Cell").Reset
            With Application.CommandBars("Cell")
                .Controls("Paste").OnAction = "PasteValues" ' Changes right click cell paste to use Pastevalues macro
                
            End With
    
        Application.CommandBars("Row").Reset
            With Application.CommandBars("Row")
                .Controls("Paste").OnAction = "PasteValues" ' Changes right click row paste to use Pastevalues macro
                
            End With
    
        Application.CommandBars("Column").Reset
            With Application.CommandBars("Column")
                .Controls("Paste").OnAction = "PasteValues" ' Changes right click column paste to use Pastevalues macro
                
            End With
    
        Application.CommandBars("Worksheet Menu Bar").Reset
        With Application.CommandBars("Worksheet Menu Bar").Controls
            .Item(2).Controls(6).OnAction = "PasteValues"  ' changes edit/paste to use Pastevalues macro
            .Item(2).Controls(7).Enabled = False ' disable paste special
        End With
    
        Application.CommandBars("Standard").Reset
        With Application.CommandBars("Standard").Controls.Item(12)
            .Enabled = False
        End With
        If Workbook_Deactivate Then End Sub
    End Sub
    Sub pastevalues()
    On Error Resume Next
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    
    End Sub
    This is what I have in ThisWorkBook to disable/enable cut, delete and cellDragDrop.

    Private Sub Workbook_Activate()
        Dim ctrl As Office.CommandBarControl
        For Each ctrl In Application.CommandBars.FindControls(ID:=21) 'Disable Cut
            ctrl.Enabled = False
        Next ctrl
            For Each ctrl In Application.CommandBars.FindControls(ID:=292) 'Disable Delete
                ctrl.Enabled = False
            Next
            Application.CellDragAndDrop = False
            
    End Sub
    Private Sub Workbook_Deactivate()
        Dim ctrl As Office.CommandBarControl
        For Each ctrl In Application.CommandBars.FindControls(ID:=21) 'Enable Cut
            ctrl.Enabled = True
        Next ctrl
            For Each ctrl In Application.CommandBars.FindControls(ID:=292) 'Enable Delete
                ctrl.Enabled = True
            Next
            Application.CellDragAndDrop = True
            
    End Sub
    Thanks for the help.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    re: disabling macros when closing WB

    I would suppose you would need a workbook_close event that resets everything when you close the workbook.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: disabling macros when closing WB

    You really should not mess with user settings with your code, it can lead to many problems as you are beginning to find out.

    As Dave says try putting your restore code into the workbook close event as well as de-activate
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    01-01-2010
    Location
    Florida
    MS-Off Ver
    Excel 2003, 2007
    Posts
    5

    Re: disabling macros when closing WB

    Thank you for your response. I am aware that changing user settings could lead to problems but this is the only way I can make it user friendly.

    Is there a code to reset all to default? or can someone walk me through how to reverse my paste special code so that I may add to my deactivate sub or close event. All the code is listed.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: disabling macros when closing WB

    Quote Originally Posted by orbatrin View Post
    Thank you for your response. I am aware that changing user settings could lead to problems but this is the only way I can make it user friendly.
    How does changing excelmake it more user friendly. You really should not do this unless your Excel VBA skills are above basic.

    This example contains allthe code to remove cut,copy & paste & restore it
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: disabling macros when closing WB

    Hi RoyUK,

    Is it possible to re-assign paste functionality/shortcut/key to pastespecial xlvalues.
    I often have a problem where users paste in data, and corrupt cell formatting and validation.

    Any help would be appreciated.
    Thanks.

  7. #7
    Registered User
    Join Date
    01-01-2010
    Location
    Florida
    MS-Off Ver
    Excel 2003, 2007
    Posts
    5

    Re: disabling macros when closing WB

    royUK,

    Thank you for the sample code that you posted, gives me an idea of more possibilities to activate and deactivate macros. Unfortunately the code does not look like it will override my paste special macros. that code is good to disable and enable cut, copy, and paste.

    Just an idea...is it possible to reassign I.D's with a different control I.D and then reverse it at Workbook_Deactivate?? this would be a more simplified way to reassign Paste with Paste Special/values.

    WotaDude is correct. Users are cutting/copying formulas and corrupting data..there has to be a more simplified way to override controls in specified workbooks. Any help would be greatly appreciated.

+ 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