Solved

VBA to Delete empty COLS in a spreadsheet

Posted on 2014-02-06
2
287 Views
Last Modified: 2014-02-06
I receive hundreds of messy worksheets from our customers each month.  I need to convert this data to a list format for pivot table analysis and recording.  Most contain merged cells, blank rows, subtotals and empty columns that I must remove.  I am looking for some simple, flexible VBA code that will help me get a jump on each of these files.

Here's what I'm thinking:

Step1      Select entire sheet and remove merged cells
Step2      In the block of data, delete any COLS without headers in the first row
Step3      In the block of data, delete any COLS with headers but no data in ROW2
Step4      Delete any rows without data in the COL A

Attached is a slice of a typical example.

Thanks for your help, Experts!
DeleteBlankColsRows.xlsx
0
Comment
Question by:thutchinson
2 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39840397
Added this macro to your workbook:

so if you run "FormatSheet" macro, it will execute against the currently active worksheet.


Option Explicit
Sub FormatSheet()
    Dim myLastCell As Range
    Dim i As Long
    
    
    Set myLastCell = LastCell(ActiveSheet)
    ActiveSheet.Cells.UnMerge
       
    For i = myLastCell.Column To 1 Step -1
        If Cells(1, i).Value = "" Then
            Cells(1, i).EntireColumn.Delete
        End If
    Next
    
    For i = myLastCell.Row To 2 Step -1
        If Cells(i, 1).Value = "" Then
            Cells(i, 1).EntireRow.Delete
        End If
    Next

End Sub

Function LastCell(ws As Worksheet) As Range

 '
' Note "&" denotes a long value; "%" denotes an integer value
  
    Dim LastRow&, lastCol%

' Error-handling is here in case there is not any
' data in the worksheet

    On Error Resume Next

    With ws

  ' Find the last real row

    LastRow& = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

  ' Find the last real column

    lastCol% = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column

    End With

' Finally, initialize a Range object variable for
' the last populated row.
    
    Set LastCell = ws.Cells(LastRow&, lastCol%)


End Function

Open in new window

DeleteBlankColsRows.xlsm
0
 

Author Closing Comment

by:thutchinson
ID: 39840495
Ken,

That procedure is sweet!   Thank you so much.

Best regards,
-Todd
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel 2013: Default workbook 13 28
how to delete specific files and folders with VBA 3 25
Best Excel  formula for  this scenario 2 36
Excel formula Sumif not working 4 28
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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