assign a macro to a coding

azad092

Board Regular
Joined
Dec 31, 2019
Messages
198
Office Version
  1. 2007
Platform
  1. Windows
hi
dear all
good morning
I am using the following code to prevent the copy/cut and paste command by the user which is working smoothly.

I want to use this code with a macro....guide me how I can add a macro to enable this coding and another macro to disable this coding

Code:
Private Sub Workbook_Activate()
Application.CutCopyMode = False
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
MsgBox "You Are Not Allowed To Paste Anything"" " & vbNewLine & "For Futher Help Contact Admin", , "PES RESCUE 1122 MULTAN"
End Sub
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Application.CutCopyMode = False
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.CutCopyMode = False
End Sub
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,
You can make your codes a common procedure that you pass argument to (on / off switch) that you can call from anywhere in your project

Place following code is a STANDARD module

VBA Code:
Sub SetDragAndDrop(ByVal AllowDragAndDrop As Boolean, Optional ByVal ShowMsg As Boolean)

    With Application
       If AllowDragAndDrop Then .OnKey "^c" Else .OnKey "^c", ""
        .CellDragAndDrop = AllowDragAndDrop
        .CutCopyMode = False
    End With
    If ShowMsg Then MsgBox "You Are Not Allowed To Paste Anything"" " & vbNewLine & _
                            "For Futher Help Contact Admin", 64, "PES RESCUE 1122 MULTAN"
End Sub

I have included Optional argument to allow display of your msgbox if required - default is False (do not display)
Placing this code in a standard module, you can now call it as required from within your project.


You can now update your event codes in the Thisworkbook code page as follows

VBA Code:
Private Sub Workbook_Activate()
    SetDragAndDrop AllowDragAndDrop:=False, ShowMsg:=True
End Sub
Private Sub Workbook_Deactivate()
    SetDragAndDrop AllowDragAndDrop:=True
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    SetDragAndDrop AllowDragAndDrop:=False
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    SetDragAndDrop AllowDragAndDrop:=True
End Sub

Note: I have shown the Parameter names to make it clearer to you what action is being taken when calling the procedure but you can omit doing this & just pass the values to shorten the code if you prefer

example

VBA Code:
SetDragAndDrop False, True


Hope Helpful

Dave
 
Upvote 0
1. Another way to write your code avoiding repitition (place in ThisWorkook code window) :
VBA Code:
Private Sub Workbook_Activate()
    SwitchOn
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    SwitchOn
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    SwitchOn
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    SwitchOn
End Sub

Private Sub Workbook_Deactivate()
    SwitchOff
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    SwitchOff
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    SwitchOff
End Sub

Private Sub SwitchOn()
    Application.CutCopyMode = False
    Application.OnKey "^c", ""
    Application.CellDragAndDrop = False
End Sub
Private Sub SwitchOff()
    Application.CutCopyMode = False
    Application.OnKey "^c"
    Application.CellDragAndDrop = True
End Sub
.
.

2. This does not answer your question but may provide a different way for you to achieve what you want
- what EXACTLY do you want to be able to do within the workbook when the switch is turned off?
- it may be easier to write a macro to give you that ability
 
Upvote 0
1. Another way to write your code avoiding repitition (place in ThisWorkook code window) :
VBA Code:
Private Sub Workbook_Activate()
    SwitchOn
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    SwitchOn
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    SwitchOn
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    SwitchOn
End Sub

Private Sub Workbook_Deactivate()
    SwitchOff
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    SwitchOff
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    SwitchOff
End Sub

Private Sub SwitchOn()
    Application.CutCopyMode = False
    Application.OnKey "^c", ""
    Application.CellDragAndDrop = False
End Sub
Private Sub SwitchOff()
    Application.CutCopyMode = False
    Application.OnKey "^c"
    Application.CellDragAndDrop = True
End Sub
.
.

2. This does not answer your question but may provide a different way for you to achieve what you want
- what EXACTLY do you want to be able to do within the workbook when the switch is turned off?
- it may be easier to write a macro to give you that ability
thanks for your attention
but i mean that i want to use this coding with a macro/command button
so review it again and then guide me thanks
 
Upvote 0
I will if you answer my question
yes exactly I want to use macro to prevent the copy/cut and paste command ....the above mentioned code works very well but whenever i want to enable the copy/cut and paste command I have to open the VBA explorer and have to stop or delete the coding... So I also want that instead of doing this I just run a macro to enable/disable these commands...... Dear Member I have explained you and i hope you got it... thanks again for help
 
Upvote 0
This is why I asked what you want to do ....

Cell Drag&Drop is not a problem
Use a toggle button to toggle between Application.CellDragAndDrop = True / Application.CellDragAndDrop = False

Copy, Cut and Paste is different
- the marching ants cause a problem
- they dissappear between copy(cut) and paste if interrupted by the event macros
- which prevents paste from working :(

Marching Ants.jpg


Workaround
The simplest way is to write macros to allow you to do what you want

Example to copy a range - place this in a module and call it from a button
VBA Code:
Sub CopyRange()
    Dim copyRng, pasteCell As Range
    Set copyRng = Application.InputBox("Select range to copy and click OK", "COPY", , , , , , 8)
    Set pasteCell = Application.InputBox("Select paste cell & click OK", "PASTE", , , , , , 8)
    copyRng.Copy pasteCell
End Sub

Macro requires tailoring to allow paste values etc ...
 
Upvote 0
This is why I asked what you want to do ....

Cell Drag&Drop is not a problem
Use a toggle button to toggle between Application.CellDragAndDrop = True / Application.CellDragAndDrop = False

Copy, Cut and Paste is different
- the marching ants cause a problem
- they dissappear between copy(cut) and paste if interrupted by the event macros
- which prevents paste from working :(

View attachment 12255

Workaround
The simplest way is to write macros to allow you to do what you want

Example to copy a range - place this in a module and call it from a button
VBA Code:
Sub CopyRange()
    Dim copyRng, pasteCell As Range
    Set copyRng = Application.InputBox("Select range to copy and click OK", "COPY", , , , , , 8)
    Set pasteCell = Application.InputBox("Select paste cell & click OK", "PASTE", , , , , , 8)
    copyRng.Copy pasteCell
End Sub

Macro requires tailoring to allow paste values etc ...
bundle of thanks dear
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

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