Option Explicit
Private WithEvents AppEvents As Application
#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
#End If
'Change this Const as required.
Const SHEET_WHERE_DRAG_N_DROP_IS_DISABLED = "Sheet1"
Private Sub Workbook_Activate()
Set AppEvents = Application
End Sub
Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook)
EnableCellDrafAndDrop = Not (Wb Is ThisWorkbook And ActiveSheet Is Sheets(SHEET_WHERE_DRAG_N_DROP_IS_DISABLED))
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
EnableCellDrafAndDrop = Not (Sh Is Sheets(SHEET_WHERE_DRAG_N_DROP_IS_DISABLED))
End Sub
Private Property Let EnableCellDrafAndDrop(ByVal Enable As Boolean)
On Error GoTo CloseClipbrd
Call OpenClipboard(Application.hwnd)
Application.CellDragAndDrop = Enable
CloseClipbrd:
Call CloseClipboard
End Property
Please can I see your code for this? I wouldn't mind this solution at all. Thank you.Jon
Thank you for your suggestion. Unfortunately I need Drag/Drop enabled on most of the other sheets. I could adopt your suggestion and activate on all the other sheets. However I am approching 50 sheets now and this is quite a task.
By careful use of the change event I can get close to what I need. Drag/Drop becomes disabled after the first change. As long as the user doesn't make an illegal "drag" the first change I can avoid them screwing up the logic on this sheet. Its far from ideal but does work somewhat.
Once again many thanks
BG
Hi, Thanks for this Alternative solution, but is it possible to have this code work for all the sheets in the workbook?@hadihakimzadeh
See if this works for you :
The code assumes Sheet1 is the sheet where CellDragAndDrop is disabled... Change as required.
Code in the Thisworkbook Module
VBA Code:Option Explicit Private WithEvents AppEvents As Application #If VBA7 Then Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long #Else Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long #End If 'Change this Const as required. Const SHEET_WHERE_DRAG_N_DROP_IS_DISABLED = "Sheet1" Private Sub Workbook_Activate() Set AppEvents = Application End Sub Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook) EnableCellDrafAndDrop = Not (Wb Is ThisWorkbook And ActiveSheet Is Sheets(SHEET_WHERE_DRAG_N_DROP_IS_DISABLED)) End Sub Private Sub Workbook_SheetActivate(ByVal Sh As Object) EnableCellDrafAndDrop = Not (Sh Is Sheets(SHEET_WHERE_DRAG_N_DROP_IS_DISABLED)) End Sub Private Property Let EnableCellDrafAndDrop(ByVal Enable As Boolean) On Error GoTo CloseClipbrd Call OpenClipboard(Application.hwnd) Application.CellDragAndDrop = Enable CloseClipbrd: Call CloseClipboard End Property
Are you using just one workbook ?Hi, Thanks for this Alternative solution, but is it possible to have this code work for all the sheets in the workbook?
Are you using just one workbook ?
If so, just disable the application CellDragAndDrop Property manually via the User Interface... You don't need code.
Option Explicit
Private WithEvents AppEvents As Application
#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
#End If
'Change this Const as required.
Const DNDSheet = "CONFIG"
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please do NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
Private Sub Workbook_Activate()
Set AppEvents = Application
End Sub
Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook)
EnableCellDragAndDrop = Not (Wb Is ThisWorkbook And ActiveSheet Is Sheets(DNDSheet))
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
EnableCellDragAndDrop = Not (Sh Is Sheets(DNDSheet))
End Sub
Private Property Let EnableCellDragAndDrop(ByVal Enable As Boolean)
On Error GoTo clipboardC
Call OpenClipboard(Application.hwnd)
Application.CellDragAndDrop = Enable
clipboardC:
Call CloseClipboard
End Property
Option Explicit
Private WithEvents AppEvents As Application
#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
#End If
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please do NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
Private Sub Workbook_Activate()
Set AppEvents = Application
End Sub
Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook)
EnableCellDragAndDrop = Not (Wb Is ThisWorkbook)
End Sub
Private Property Let EnableCellDragAndDrop(ByVal Enable As Boolean)
On Error GoTo clipboardC
Call OpenClipboard(Application.hwnd)
Application.CellDragAndDrop = Enable
clipboardC:
Call CloseClipboard
End Property
Thank you so much. This works quite nicely. I added some other code to wipe the clipboard on exit tho, because I didn't want the notifications that showed after I exited the file, having done lots of copy & paste actions.Try this:
VBA Code:Option Explicit Private WithEvents AppEvents As Application #If VBA7 Then Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long #Else Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long #End If Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range) Select Case Application.CutCopyMode Case Is = False 'do nothing Case Is = xlCopy 'do nothing Case Is = xlCut MsgBox "Please do NOT Cut and Paste. Use Copy and Paste; then delete the source." Application.CutCopyMode = False 'clear clipboard and cancel cut End Select End Sub Private Sub Workbook_Activate() Set AppEvents = Application End Sub Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook) EnableCellDragAndDrop = Not (Wb Is ThisWorkbook) End Sub Private Property Let EnableCellDragAndDrop(ByVal Enable As Boolean) On Error GoTo clipboardC Call OpenClipboard(Application.hwnd) Application.CellDragAndDrop = Enable clipboardC: Call CloseClipboard End Property
"Code is also a little slow...takes about a second or two to resolve when I activate the workbook that I want to paste data in."
I can't do anything about that I am afraid.
Option Explicit
#If VBA7 Then
'64 bit declares here
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr
#Else
'32 bit declares here
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
#End If
Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function
Option Explicit
Private WithEvents AppEvents As Application
#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
#End If
Private Sub Workbook_Activate()
Set AppEvents = Application
End Sub
Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook)
EnableCellDragAndDrop = Not (Wb Is ThisWorkbook)
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ClearClipboard
End Sub
Private Property Let EnableCellDragAndDrop(ByVal Enable As Boolean)
On Error GoTo clipboardC
Call OpenClipboard(Application.hwnd)
Application.CellDragAndDrop = Enable
clipboardC:
Call CloseClipboard
End Property
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please do NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False
End Select
End Sub
Option Explicit
Private WithEvents AppEvents As Application
#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
#End If
Private Sub Workbook_Activate()
Set AppEvents = Application
End Sub
Private Sub AppEvents_WorkbookActivate(ByVal Wb As Workbook)
EnableCellDragAndDrop = Not (Wb Is ThisWorkbook)
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ClearClipboard
End Sub
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please do NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False
End Select
End Sub
Private Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function
Private Property Let EnableCellDragAndDrop(ByVal Enable As Boolean)
On Error GoTo clipboardC
Call OpenClipboard(Application.hwnd)
Application.CellDragAndDrop = Enable
clipboardC:
Call CloseClipboard
End Property