0

Hello I am trying to delete rows in the range from 7 to 31 if the data in column A is blank. However i want this to run all worksheets other than the ones i specify. I have attempted the code but it doesn't work, can anyone give me a pointer on where i went wrong? Also should i be putting an error handling for if there isn't any blank cells?

Sub deletespaces()
Dim ws As Worksheet
Dim rng As Range

For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Menu" And ws.Name <> "Paste here" And ws.Name <> "Data" Then

 Range("a7:A31").Select
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 End If
Next ws
End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
Classmaz
  • 39
  • 5

1 Answers1

1
  1. Avoid Select.
  2. Qualify the Range with the worksheet ws.
  3. Test first for blank cells.
If ws.Name <> "Menu" And ws.Name <> "Paste here" And ws.Name <> "Data" Then
    Dim visibleCells As Range  
    Set visibleCells = Nothing

    On Error Resume Next
    Set visibleCells = ws.Range("a7:A31").SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If Not visibleCells Is Nothing Then
        visibleCells.EntireRow.Delete
    End If
End If
BigBen
  • 46,229
  • 7
  • 24
  • 40