Solved

VBA to Delete empty COLS in a spreadsheet

Posted on 2014-02-06
2
295 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

837 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