delete empty rows & columns

zhfgtj27

New Member
Joined
Sep 20, 2013
Messages
26
I have a massive spreadsheet, with data range starting from C3. The first 2 rows and columns are the headers. I want to create a macro to delete the rows and columns that are empty at the data range. I appreciate all the help.

Thanks,
Fan
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I have a massive spreadsheet, with data range starting from C3. The first 2 rows and columns are the headers. I want to create a macro to delete the rows and columns that are empty at the data range. I appreciate all the help.

Thanks,
Fan
hi,

as alternative, here's a code you might like to try
Code:
Sub del_empty_rows_cols()

Dim a As Range, u(), v()
Dim r As Long, c As Long, i As Long
Dim x As Long, y As Long

Set a = Cells
r = a.Find("*", , , , xlByRows, xlPrevious).Row
c = a.Find("*", , , , xlByColumns, xlPrevious).Column
ReDim u(1 To r, 1 To 1), v(1 To 1, 1 To c)

For i = 3 To r
    If a(i, 2).End(2).Column = Columns.Count Then
        u(i, 1) = 1
        x = x + 1
    End If
Next i

For i = 3 To c
    If a(2, i).End(4).Row = Rows.Count Then
        v(1, i) = 1
        y = y + 1
    End If
Next i

a(c + 1).Resize(r) = u
a(r + 1, 1).Resize(, c) = v

With a(1).Resize(r + 1, c + 1)
    .Sort a(c + 1), Orientation:=xlTopToBottom, Header:=xlNo
    .Sort a(r + 1, 1), Orientation:=xlLeftToRight, Header:=xlNo
    If x > 0 Then .Rows(1).Resize(x).Delete xlUp
    If y > 0 Then .Columns(1).Resize(, y).Delete xlToLeft
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top