Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA to Delete empty COLS in a spreadsheet

Posted on 2014-02-06
2
Medium Priority
?
312 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

670 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