

If oParentForm.DragSource Is LstBx Then Exit Sub Private Sub LstBx_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As Long, ByVal Data As MSForms.DataObject, ByVal x As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer) Private WithEvents LstBx As MSForms.ListBoxįriend Property Set ParentForm(ByRef oValue As clDragDropForm)įriend Property Set DragDropListBox(ByRef oValue As MSForms.ListBox)
#VBA DRAG AND DROP FILE EXCEL CODE#
This is the code of class clDragDropListBox: '. The necessary other objects of class clDragDropListBox are then automatically created from this class. To set day and drop for the list boxes on a form, an instance of this class is created with a few lines of code. Set oDragDropListBox = New clDragDropListbox Private lstbxDragSource As MSForms.ListBoxįriend Property Set DragDropForm(ByRef oFrm As Object)įriend Property Set DragSource(ByRef oListBox As MSForms.ListBox)ĭim ctl As Control, oDragDropListBox As clDragDropListbox ' Purpose : Set up ListBox Drag & Drop Basic ' Copyright : © 2020, all rights reserved The code of class clDragDropForm is as follows: '.

The other class clDragDropListBox is used to handle the listbox events. The first class clDragDropForm links to the form.

The basic setup for enabling drag and drop consists of 2 class modules. An example file of both elaborations can be downloaded. This is followed by an advanced versatile version, in which the drag and drop can be very precisely configured per form. Nothing else needs to be changed on the form itself.įirst, a limited basic structure of this structure will be discussed. This way, by adding just a few lines of code to a form, drag and drop can be set for all listboxes very easily. All drag and drop logic is contained in these classes. To make it very easy to set up drag and drop for list boxes on a form, this article uses class modules. During the drag and drop, all other events on the form are disabled. After all, the actual drop is handled by the BeforeDropOrPaste event. When configured correctly, the mouse drag is handled by the BeforeDragOver event. The list items to be moved or copied are copied into a DataObject. The drag and drop is initiated in the MouseMove event. The following three events play a role in drag and drop: MouseMove, BeforeDragOver and BeforeDropOrPaste. Setting up drag and drop for list boxes on a VBA form is not very easy and requires very careful work. Dealing with calculation errors in excel.Pitfalls setting conditional format with VBA.
