Sort a list by drag and drop using VBA event handlers

kienleong

New Member
Joined
Mar 17, 2010
Messages
13
Hello All,

I have built a sheet that allows the user to sort a table of data. Rather than using sort values, I am replicating a drag-and-drop with a single click to select the row and another to insert it at another place in the table.

The best way I can come up with uses worksheet_SelectionChange like so:

Code:
Global OldValue as Variant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim LastCol As Long, LastRow As Long
    Dim NewRow As Long
    
    LastCol = Me.Cells(1, 1).End(xlToRight).Column
    LastRow = Me.Cells(1048576, 1).End(xlUp).Row
    
    If Target.Column <> LastCol Or _
        Target.Row > LastRow Then Exit Sub
    
    If Application.CutCopyMode = False Then
        OldValue = Target.Row
        Me.Range(Cells(OldValue, 1), Cells(OldValue, LastCol)).Copy
    Else
        NewRow = Target.Row
        If OldValue > NewRow Then OldValue = OldValue + 1
        Me.Cells(NewRow, 1).Insert shift:=xlDown
        Application.CutCopyMode = False
        Me.Rows(OldValue).Delete
    End If
  
End Sub
This works fine, but it uses Global variables. I understand they are not best practice and should be avoided if possible.

Is there any way to do this without using Global variables?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You could try this instead

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldValue As Variant

    Dim LastCol As Long, LastRow As Long
    Dim NewRow As Long
    
    LastCol = Me.Cells(1, 1).End(xlToRight).Column
    LastRow = Me.Cells(1048576, 1).End(xlUp).Row
    
    If Target.Column <> LastCol Or _
        Target.Row > LastRow Then Exit Sub
    
    If Application.CutCopyMode = False Then
        OldValue = Target.Row
        Me.Range(Cells(OldValue, 1), Cells(OldValue, LastCol)).Copy
    Else
        NewRow = Target.Row
        If OldValue > NewRow Then OldValue = OldValue + 1
        Me.Cells(NewRow, 1).Insert shift:=xlDown
        Application.CutCopyMode = False
        Me.Rows(OldValue).Delete
    End If
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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