Search code examples
excelvba

Delete rows that are part of merged cells


I have the following Excel table:
enter image description here

I would like to delete all the lines where "HFO", for example, is written (rows 16-18 in this case).

Is there any way to do it or to identify how many rows are part of a merged cell?

I have seen Delete rows with merged cells. It doesn't work for me because rows 10-15 also have merged cells and I don't want to delete them.


Solution

  • MergeArea returns a Range object referenced to all merged cells. Because you need to delete the rows, then add EntireRow object:

    enter image description here

    Dim rng As Range
    
    For Each rng In Range("C16:C24")
        If rng.Value = "HFO" Then rng.MergeArea.EntireRow.Delete
    Next rng
    

    Output after code:

    enter image description here

    Notice we deleted only those rows with merged cells and value "HFO"