Solved

VBA to Delete empty COLS in a spreadsheet

Posted on 2014-02-06
2
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

752 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