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
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: