Error handling when data is deleted/cleared

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I have the following code that works well until someone accidentally loads incorrect data and needs to delete/clear the data

VBA Code:
Sub Mod01()

Application.ScreenUpdating = False

Dim lr As Long
lr = Worksheets("PP01 LD").Cells(Rows.Count, "X").End(xlUp).Row ' change column reference to suit


    Worksheets("PP01 Mod").Activate
    Worksheets("PP01 Mod").Range("A3:D3").Select
    Selection.AutoFill Destination:=Range("A3:D" & lr), Type:=xlFillDefault
    
    Worksheets("PP01 LD").Activate
    
Application.ScreenUpdating = True

End Sub

Can someone help me add either an error handling or additional code where if data in PP01 LD sheet gets deleted/cleared only range A4:D & lr gets cleared out on sheet PP01 Mod? Any worksheet change in PP01 LD triggers this code but currently when someone deletes/clears data cells A1:D3 copy the cells in A3:D3 but they're already prefilled with column headers and row 2 with a different formula set.

Thanks in advance for your assistance here!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Solved! :p

VBA Code:
Sub Mod01()

Application.ScreenUpdating = False

Dim lr As Long
lr = Worksheets("PP01 LD").Cells(Rows.Count, "X").End(xlUp).Row ' change column reference to suit

    Worksheets("PP01 Mod").Activate
    Worksheets("PP01 Mod").Range("A3:D3").Select
        If lr = 1 Then
                Worksheets("PP01 Mod").Range("A4:D2000").ClearContents
            Else
                Selection.AutoFill Destination:=Range("A3:D" & lr), Type:=xlFillDefault
        End If
    Worksheets("PP01 LD").Activate
    
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Uggghhhh did a dummy...

VBA Code:
Sub Mod01()

Application.ScreenUpdating = False

Dim lr As Long
Dim lr2 As Long
lr = Worksheets("PP01 LD").Cells(Rows.Count, "X").End(xlUp).Row ' change column reference to suit
lr2 = Worksheets("PP01 Mod").Cells(Rows.Count, "A").End(xlUp).Row

    Worksheets("PP01 Mod").Activate
    Worksheets("PP01 Mod").Range("A3:D3").Select
        If lr = 1 Then
                Worksheets("PP01 Mod").Range("A4:D" & lr2).ClearContents
            Else
                Selection.AutoFill Destination:=Range("A3:D" & lr), Type:=xlFillDefault
        End If
    Worksheets("PP01 LD").Activate
    
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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