0

I am trying to come up with a code that will delete all entries of a row if the corresponding B cell of the row is empty.

For example, if B4 is empty the code should delete all entries in C4:AU4. It should do this for all rows starting from B3 to a beforehand defined value for MaxRowList.

Only thing I could come up with so far is

Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

But this deletes the complete row from the worksheet which screws up the arrangement. It is essential that it only deletes the entries in the corresponding $C$:$AU$ rangeand doesn't remove the row.

3 Answers 3

2

Just use Intersect and ClearContents:

Sub tgr()

    With ActiveWorkbook.ActiveSheet
        Intersect(.Range("C:AU"), .Columns("B").SpecialCells(xlCellTypeBlanks).EntireRow).ClearContents
    End With

End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

I assume that you want to clear the content of the cells, not delete them (which would move to content of the cells below or to the right)? Try

dim ws as worksheet
set ws = activesheet ' <== Replace with whatever sheet you are working.
Dim emptyCells As Range, cell As Range
Set emptyCells = ws.Columns("B:B").SpecialCells(xlCellTypeBlanks)
For Each cell In emptyCells
    Dim r As Range
    Set r = ws.Range("C" & cell.row & ":AE" & cell.row)
    r.Clear
Next cell

Comments

1

Not sure it's possible to avoid a loop. Silly me, of course it is - see @tigeravatar.

Sub x()

Dim r As Range

For Each r In Columns("B:B").SpecialCells(xlCellTypeBlanks).Offset(, 1)
    r.Resize(, 45).Clearcontents
Next r

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.