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 everything in C9:K9.

 

Looking at the image uploaded, when I delete "811HT-24", it would then delete RCC,x,7458,1x24,2x24,3x32,1x48 if it worked like I'm hoping.

 

In the B column I'm using VLOOKUP tied to an index. All of the sheets are identical as well. 

Any help would be appreciated. 

  • 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.

4 Replies

  • 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.

    • td0g906's avatar
      td0g906
      Copper Contributor

      It's odd. My replies don't show up. Anyways, changing "B:B" to "A:A" gave me the result I was looking for. Thank you!

    • td0g906's avatar
      td0g906
      Copper Contributor

      After switching the "B:B" to "A:A" I'm getting the result I was looking for. Thank you!!

    • td0g906's avatar
      td0g906
      Copper Contributor

      It wasn't working initially, but then I changed "B:B" to "A:A" and I'm getting the result I was looking for. Thank you!!

Resources