Delete macro for blank rows/column on selected tabs

Posted on 2013-10-14
Medium Priority
Last Modified: 2013-10-16
Assume that there are two tabs with data on a file and each tab has a different number of rows and columns populated. What I need to do is to delete all the rest of the blank rows and columns from those tabs via a macro run.  Can I get a working sample for that please?  Thank you in advance.
Question by:trusxlsol
LVL 81

Expert Comment

ID: 39572629
You might try a macro like this to delete your blank rows and columns. It works on the active worksheet.
Sub DeleteEmptyRowsAndColumns()
Dim rg As Range
Dim i As Long, j As Long
Application.ScreenUpdating = False
Set rg = ActiveSheet.UsedRange
For i = rg.Rows.Count To 1 Step -1
    If Application.CountA(rg.Rows(i)) = 0 Then rg.Rows(i).Delete
For j = rg.Columns.Count To 1 Step -1
    If Application.CountA(rg.Columns(j)) = 0 Then rg.Columns(j).Delete
End Sub

Open in new window

LVL 31

Accepted Solution

gowflow earned 800 total points
ID: 39573304

This macro should delelte all blank rows 'below' and colmns 'after to the right' of your existing data in all of the worksheets in your current workbook.

I have put in the attached workbook some data for trial. Simply make sure your macros are activated and choose to run the macro called DeleteBlankRowsCols

Sub DeleteBlankRowsCols()
Dim WS As Worksheet
Dim MaxRow As Long, MaxCol As Long
Dim Rng As Range, RngtoDel As Range

For Each WS In ActiveWorkbook.Worksheets
    '---> Get LastRow and LastCol for that sheet
    Set Rng = WS.UsedRange
    MaxRow = Rng.Rows.Count
    MaxCol = Rng.Columns.Count
    '---> Delete All Columns after the usedrange
    Set RngtoDel = WS.Range(WS.Cells(1, MaxCol + 1), WS.Cells(1, WS.Columns.Count))
    '---> Delete All Rows after the usedrange
    Set RngtoDel = WS.Range(WS.Cells(MaxRow + 1, 1), WS.Cells(WS.Rows.Count, 1))

Next WS

MsgBox ("All blank Rows and Columns Successfully deleted in all sheets of this workbook.")

End Sub

Open in new window


Author Closing Comment

ID: 39577623
Thank you!

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Currently, there is an issue with being able to copy values from an external application to a dropdown list in Project Web Access (PWA).  The standard copy and paste methods don't seem to work properly. Here is a way to accomplish this task to s…
What to do if a split doesn't fit? Or a bunch of invoice lines must be rounded while the sum must match a total? It takes a little, but - when done - it is extremely easy to implement.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question