Private Sub Worksheet_Change(ByVal Target As Range)
'
Dim UndoString As String
Dim SelectionRow As Long
Dim EndDragColumnLetter As String, StartDragColumnLetter As String
Dim TargetAddressArray As Variant
'
If Target.CountLarge < 2 Then Exit Sub ' If only 1 cell has been selected by user, allow the change
If Application.WorksheetFunction.CountA(Target) = 0 Then Exit Sub ' Allow deletetions via the delete key
'
SelectionRow = Selection.Row ' Save the Selection.Row into SelectionRow
'
TargetAddressArray = Split(Selection.Address(0, 0), ":") ' Separate Address Range into TargetAddressArray
'
EndDragColumnLetter = Evaluate(Replace("=LEFT(""X"",MIN(FIND({0,1,2,3,4,5,6,7,8,9}" & _
",ASC(""X"")&1234567890))-1)", "X", TargetAddressArray(1))) ' Get EndDragColumnLetter of TargetAddressArray(1)
'
StartDragColumnLetter = Evaluate(Replace("=LEFT(""X"",MIN(FIND({0,1,2,3,4,5,6,7,8,9}" & _
",ASC(""X"")&1234567890))-1)", "X", TargetAddressArray(0))) ' Get StartDragColumnLetter of TargetAddressArray(0)
'
If Not Intersect(Target, Range("E3:E42")) Is Nothing And Target.CountLarge > 1 Or _
Not Intersect(Target, Range("F3:F42")) Is Nothing And Target.CountLarge > 1 Or _
Not Intersect(Target, Range("G3:G42")) Is Nothing And Target.CountLarge > 1 Then ' If change was made to ranges we are monitoring then ...
'
With Application
.EnableEvents = False ' Turn Events off to prevent potential code loop
'
If .CommandBars("Standard").Controls("&Undo").List(1) = "Paste" And _
.CutCopyMode = xlCopy Then ' If Copy/paste detected then ...
.Undo ' Undo the change that was made by the user
'
Target.PasteSpecial Paste:=xlPasteValues ' Do the Copy/paste as pastespecial PasteValues
.CutCopyMode = False ' Clear the clipboard & 'Marching Ants'
.EnableEvents = True ' Turn Events back on
Exit Sub
End If
'
.Undo ' Undo the change that was made by the user
'
If SelectionRow = ActiveCell.Row Then ' If Dragging down then ...
Range(TargetAddressArray(0) & ":" & EndDragColumnLetter & _
ActiveCell.Row).AutoFill Destination:=Range(TargetAddressArray(0) & _
":" & TargetAddressArray(1)), Type:=xlFillValues ' Make the user changes without affecting format
Else ' Else ...
Range(StartDragColumnLetter & ActiveCell.Row & ":" & EndDragColumnLetter & _
ActiveCell.Row).AutoFill Destination:=Range(TargetAddressArray(1) & _
":" & TargetAddressArray(0)), Type:=xlFillValues ' Make the user changes without affecting format
End If
'
.EnableEvents = True ' Turn Events back on
End With
End If
End Sub