Home » excel » excel – Delete cells if job sheet no longer exists

excel – Delete cells if job sheet no longer exists

Posted by: admin April 23, 2020 Leave a comment

Questions:

Everyone;

I have this bit of code that copies data from worksheets with a numeric name (a job number) and pasts them into a column +1 on a master sheet to be email off every night.

I want to delete job pages when the jobs are complete. When I delete a page, my data isn’t being removed from the Master Email sheet.

How can I modify this code to delete from cells 18-39 in column XXX if the value in cell 19 (the job number) is no longer in the workbook?

Thanks for any help!

Private Sub Worksheet_Activate()
    Dim ws, MASTER111 As Worksheet
    Dim ColIndex As Integer
    Dim CopyRange As Range
    Set Master = ThisWorkbook.Sheets("MASTER111")

    ColIndex = 2 'first column on Master sheet to paste data
    For Each ws In ThisWorkbook.Worksheets
        If IsNumeric(ws.Name) Then
           Master.Cells(19, ColIndex) = ws.Range("A1") 'put job name in first row
           Set CopyRange = Intersect(ws.UsedRange, ws.Range("N:N"))
           CopyRange.Copy
           Master.Cells(16, ColIndex).PasteSpecial xlValues
           Master.Cells(18, ColIndex) = "Job #"
           Master.Cells(19, ColIndex) = ws.Range("A1") 'put job name in first row
           ColIndex = ColIndex + 1
           Set CopyRange = Nothing
        End If
    Next ws
End Sub
How to&Answers:

Taking your code as sample, if you want to clear on change of events on the desired Sheet.

Add the code to the desired Sheet. Not on Modules. e.g.: Sheet1

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo CleanExit
If Cells(2, Target.Column) = "0" Then
    Application.EnableEvents = False
    Range(Cells(4, Target.Column), Cells(8, Target.Column)).Clear
End If
CleanExit:
    Application.EnableEvents = True
    On Error GoTo 0
End Sub

And next time try to edit you question with your new code, don’t post large codes on comment.