简体   繁体   中英

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.

在此处输入图片说明

Thanks

You could use SpecialCells, eg

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM