+ Reply to Thread
Results 1 to 3 of 3

Macro that deletes empty columns with no data in all worksheets in a workbook

  1. #1
    Registered User
    Join Date
    01-27-2016
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    1

    Macro that deletes empty columns with no data in all worksheets in a workbook

    Hi All,

    I'm trying to get the macro to delete columns that don't contain any data.

    The data I'm using has 120 worksheets with several titled columns of blank data. This macro works for 1 sheet but when I try and apply it to multiple worksheets it won't work.

    Can someone tell me what I'm doing wrong?

    Sub DeleteEmptyColumns()

    Dim j As Integer
    Dim k As Integer
    Dim intLastRow As Integer
    Dim intLastCol As Integer
    Dim booColumnEmpty As Boolean
    Dim varContent As Variant
    ' for every worksheet
    Dim Ws As Worksheet

    For Each Ws In ActiveWorkbook.Worksheets

    intLastRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row
    intLastCol = Range("A1").SpecialCells(xlCellTypeLastCell).Column

    For j = 1 To intLastCol
    varContent = ""
    For k = 2 To intLastRow
    varContent = varContent & Cells(k, j)
    If varContent = "" Then
    'continue:
    Else
    GoTo SkipColumn
    End If
    Next k
    Cells(k, j).EntireColumn.Delete
    j = j - 1
    intLastCol = intLastCol - 1
    If intLastCol < 1 Then GoTo LastColumnExit
    SkipColumn:
    Next j

    LastColumnExit:
    Next Ws
    End Sub

    Thank you for any help

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Macro that deletes empty columns with no data in all worksheets in a workbook

    Code between code tags like this

    Please Login or Register  to view this content.
    Kind regards
    Leo

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Macro that deletes empty columns with no data in all worksheets in a workbook

    Hi eskay and welcome to the forum,

    Here is the code I'd use to do your problem. My assumption is that the column heads are all in the first row on each sheet.

    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Recording Macro that deletes remaining columns
    By shawnnai in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-08-2015, 06:18 PM
  2. Need help with macro that deletes all columns, except ones with certain headers
    By pavlovs2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-29-2015, 03:15 PM
  3. [SOLVED] Code that finds and deletes names from current sheet to all worksheets in workbook
    By grazian2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-01-2013, 11:51 AM
  4. Replies: 2
    Last Post: 07-31-2013, 03:21 PM
  5. Workbook.SaveAs to CSV deletes the first row if its empty.
    By arunnn in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-07-2011, 09:10 PM
  6. Macro which deletes rows stops at empty row
    By john_london in forum Excel General
    Replies: 17
    Last Post: 09-28-2010, 06:00 PM
  7. Macro That Deletes Columns
    By Lilbit in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2006, 12:25 PM

Tags for this Thread

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