+ Reply to Thread
Results 1 to 4 of 4

Deleting Empty Columns from all Worksheets

  1. #1
    Registered User
    Join Date
    03-07-2012
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    2

    Deleting Empty Columns from all Worksheets

    I have a macro which deletes empty columns from a worksheet even if that particular column has a header. It works fine, however I need it to loop through all of the worksheets in the workbook. The worksheet names can change so I can't specifically reference worksheet names in the code. Sounds simple enough but I have tried various methods and nothing seems to work. The following code will delete the empty columns on the active worksheet but then I get an error: run-time error '424' object required. Any suggestions would be greatly appreciated.


    Sub DelColumns()
    Dim ws As Worksheet, r As Range, c As Range, del As Range

    For Each ws In ActiveWorkbook.Worksheets

    Set ws = ActiveSheet
    Set r = Intersect(ws.Rows(1), ws.UsedRange)
    For Each c In r
    If WorksheetFunction.CountIf(c.EntireColumn, ">""""") = 1 Then ' or If IsEmpty(c.End(xlDown)) Then
    If del Is Nothing Then
    Set del = c
    Else
    Set del = Union(del, c)
    End If
    End If
    Next
    If Not del Is Nothing Then del.EntireColumn.Delete


    Next ws

    End Sub
    Last edited by DonaldTerry; 03-07-2012 at 03:11 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Deleting Empty Columns from all Worksheets

    Welcome to the forum.

    You should put your code within code tags (as per forum rules).
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-07-2012
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Deleting Empty Columns from all Worksheets

    Well, I had to start from scratch, but I finally figured it out on my own. The following code will identify and delete any column with a header but no other data in the column for all worksheets.


    Sub DelBlankCol()

    For Each w In Worksheets
    w.Select
    D = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    While ActiveCell.Column <= D
    If WorksheetFunction.CountA(Selection.EntireColumn) = 1 Then
    Selection.EntireColumn.Delete
    D = D - 1
    Else
    ActiveCell.Offset(0, 1).Select
    End If
    Wend
    Range("A1").Select
    Next

    End Sub

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Deleting Empty Columns from all Worksheets

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Not Quote Tags
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1