Forum Discussion

td0g906's avatar
td0g906
Copper Contributor
Nov 25, 2024
Solved

How to delete multiple cells based off the value of a specific cell.

I have an excel file with multiple sheets. I was wondering if there was a way to complete this task. When I delete the value in the column of "Hose"(A), I would like it to automatically delete ev...
  • HansVogelaar's avatar
    Nov 25, 2024

    In the desktop version of Excel for Windows or Mac:

    Press Alt+F11 to activate the Visual Basic Editor

    Double-click ThisWorkbook under Microsoft Excel objects in the Project Explorer pane on the left hand side.

    Copy the following code into the ThisWorkbook module:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Dim rng As Range
        Dim cel As Range
        Set rng = Intersect(Sh.Range("B:B"), Target)
        If Not rng Is Nothing Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            For Each cel In rng
                If cel.Value = "" And cel.Offset(0, 1).HasFormula Then
                    cel.Offset(0, 2).Resize(1, 9).ClearContents
                End If
            Next cel
            Application.EnableEvents = True
            Application.ScreenUpdating = True
        End If
    End Sub

    Switch back to Excel.

    Save the workbook as a macro-enabled workbook (*.xlsm).

    Make sure that you allow macros when you open it.

Resources