Yes, you can identify Drag and Drop Event!

tysk

New Member
Joined
Mar 11, 2013
Messages
7
I've seen a lot of people asking for this, so this is not a question but just information for you to use if you need. 'Drag and drop' is an excellent way to ruin cell reference links, so I think this info might be welcome.

This uses the fact that if you do not have a 'Application.EnableEvents = False' in your worksheet code, the change event fires twice and you can identify that. At first, I used a public counter to know if it was the first or second one, but then I found this way that is more elegant, I think.
I describe a rather clean code here, I leave out a lot of error trapping etc:
---

1. In a module you define:
Public SelectCl As String

2. In your worksheet code you use this code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If TypeName(Selection) = "Range" Then
Let SelectCl = Selection.Address
End If
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> SelectCl Then
MsgBox "## Drag and Drop identified!"
Else:
MsgBox "## NotDragDrop OR first time D&D loop"
End If
End Sub



---
You CANNOT use it together with Application.EnableEvents = False
It will not identify a drag from another workbook
It also reacts to the format painter event

Can you find any problems etc. with this, I am happy to know from you. Together with workarounds :)

Good luck,
Tyrone Skogström
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I guess this is a good candidate for using the windows API to achieve similar functionality
 
Upvote 0
Yes, I find if the users aren't within your company etc. it feel's more safe to keep the whole application within excel's functions, and you do not risk to fire any antivirus programs etc.
 
Upvote 0
@tysk's code works well for me.

If, during your drag-and-drop actions, Excel bugs you by saying
Theres-already-data-here.-Do-you-want-to-replace-it.png

this warning can be switched off in Excel's options:
Alert-before-overwriting-cells.png

Credits go to ExcelHowto. Their other solution
VBA Code:
Application.DisplayAlerts = False
did not work for me.
 
Upvote 0
Found this really useful thanks.
I also needed this to undo the last action, but due to the enabling events it was slightly more tricky.
A nice solution that worked for me was to call the application.undo part of it

Updated method here

1. In a module you define and add sub:

Public SelectCl As String

Sub UndoLastAction()
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
End Sub

2. In your worksheet code you use this code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If TypeName(Selection) = "Range" Then
Let SelectCl = Selection.Address
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Protect UserInterfaceOnly:=True
If Target.Address <> SelectCl Then
Call UndoLastAction
MsgBox "## Drag and Drop identified! - Please do not move cells as this will cause #REF errors"
Else
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,367
Members
451,642
Latest member
mirofa

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