Solved

VBA to Delete empty COLS in a spreadsheet

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
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 …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now