watchme
watchme

Reputation: 13

VBA Excel How to delete filter blank rows (but not entire row) and shift cells up

I want to delete only the blank cells (not an entire row) in a very large spreadsheet and want to shift the below cells up.

You can get an idea from the spreadsheet snapshot.

Can someone please help? I am working on very large spreadsheets.

enter image description here

Thanks

Upvotes: 0

Views: 639

Answers (2)

SJR
SJR

Reputation: 23081

You could use SpecialCells, e.g.

Sub x()
On Error Resume Next
Range("C:D").SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
'or
Range("A1").CurrentRegion.offset(,2).resize(,2).SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
On Error GoTo 0
End Sub

The On Error is to avoid an error if there are no blanks.

Upvotes: 1

Tom
Tom

Reputation: 9898

This is one way to do it. This will remove blank cells from every column on your sheet

Option Explicit
Public Sub RemoveBlanks()
    Dim rng As Range
    Dim c

    Set rng = Application.InputBox("Select a range", "Get Range", Type:=8)
    If Not rng Is Nothing Then
        On Error Resume Next
        With rng.Parent
            For Each c In rng
                Range(c, c.Offset(.Cells(.Rows.Count, c.Column).End(xlUp).Row - c.Row, 0)).SpecialCells(xlCellTypeBlanks).Delete shift:=xlUp
            Next c
        End With
        On Error GoTo 0
    Else
        MsgBox "Nothing selected"
    End If
End Sub

Upvotes: 0

Related Questions