Worksheet_Change Drag and Drop

Victor Moss

Board Regular
Joined
May 3, 2014
Messages
85
Hi Guys,

I have a Change Events running when I enter any value in Range("H5:H6000"), if all Cells in the Active Row are blank between columns A and F, the Change Event will clear the Active Cell in Column H of the same Row:

If Target.Count > 1 Then Exit Sub
If Target.Column = 8 Then
If Target.Offset(, -7).Value = "" And Target.Offset(, -6).Value = "" And Target.Offset(, -5).Value = "" And Target.Offset(, -4).Value = "" And Target.Offset(, -3).Value = "" And Target.Offset(, -2).Value = "" Then

MsgBox "My message here", vbInformation
Application.EnableEvents = False
Target.ClearContents
Target.Select
Application.EnableEvents = True
If WorksheetFunction.CountA(Cells) = 0 Then Me.ScrollArea = ""

End If
End If

The problem I'm experiencing is if I drag and drop a value in Range("H5:H6000") below where there's data, the Change Event doesn't clear the Cells where there's no data in Range("A5:F6000"), it only clear's the first cell.Count -1).

Has any experienced the same problem? Please, if you can help, it would be greatly appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

LazyBug

Board Regular
Joined
Feb 28, 2020
Messages
159
Office Version
  1. 2010
Platform
  1. Windows
Not tested:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tr&, x&, rng As Range
tr = Target.Row
Set rng = ActiveWorkbook.ActiveSheet.Range(Cells(tr, 1), Cells(tr, 6))
    If Not Intersect(Target, Range("H2:H6000")) Is Nothing Then
        x = Application.WorksheetFunction.CountA(rng)
            If x = 0 Then
                MsgBox "your message"
                Application.EnableEvents = False
                Target.ClearContents
                Application.EnableEvents = True
            End If
    End If
End Sub
 

Victor Moss

Board Regular
Joined
May 3, 2014
Messages
85
Th
Not tested:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tr&, x&, rng As Range
tr = Target.Row
Set rng = ActiveWorkbook.ActiveSheet.Range(Cells(tr, 1), Cells(tr, 6))
    If Not Intersect(Target, Range("H2:H6000")) Is Nothing Then
        x = Application.WorksheetFunction.CountA(rng)
            If x = 0 Then
                MsgBox "your message"
                Application.EnableEvents = False
                Target.ClearContents
                Application.EnableEvents = True
            End If
    End If
End Sub
Thank you very much, this works great, however, it only clears empty cells in Range("H5:H6000") if dragged from the last row of data in Range("A5:F6000"), so it only works when dragged from the last row of the used range. If cells are being dragged from the second last row or any row before that, then it doesn't clear Target cells in Column H. Is this possible?
 

Victor Moss

Board Regular
Joined
May 3, 2014
Messages
85
I've drafted an example, I'm referring to dragging the "x" in Column H below Row 10.

Change example.xlsm
ABCDEFGHIJ
5123456xIf I drag down from here, it won't clear below row 10
6123456xIf I drag down from here, it won't clear below row 10
7123456xIf I drag down from here, it won't clear below row 10
8123456xIf I drag down from here, it won't clear below row 10
9123456xIf I drag down from here, it won't clear below row 10
10123456xIf I drag down from here, it does clear below row 10
11
12
13
14
15
16
17
18
19
20
Sheet1
 

Forum statistics

Threads
1,136,284
Messages
5,674,836
Members
419,529
Latest member
TommasoP11

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
Top