+ Reply to Thread
Results 1 to 7 of 7

Delete entire column if all cells in a certain range are empty

Hybrid View

  1. #1
    Registered User
    Join Date
    10-26-2011
    Location
    York
    MS-Off Ver
    Excel 2007
    Posts
    7

    Delete entire column if all cells in a certain range are empty

    I'm not experienced with macros but I'm trying to automate a spreadsheet formatting process to save me a lot of time. One of the functions I need to perform is to delete columns where all cells within a certain range are empty. So, in columns C to FZ, if cells 3 to 1000 are all empty, delete the entire column.

    Can anyone help me with this?

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Delete entire column if all cells in a certain range are empty

    Hi,

    Try this:

    Sub test()
        Dim lngColLoop As Long
        For lngColLoop = 182 To 3 Step -1
            If Application.CountA(Cells(3, lngColLoop).Resize(998, 1)) = 0 Then
                Columns(lngColLoop).Delete
            End If
        Next lngColLoop
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete entire column if all cells in a certain range are empty

    Hi,

    Create a helper row 1 and use =IF(SUM(C4:C1001)=0,"delete","keep")

    Now use a macro along the lines of
    Sub DeleteColumns()
        Dim x As Long
        For x = Range("G1:L1").Cells.Count To 1 Step -1
            If LCase(Range("G1:L1").Cells(1, x)) = "delete" Then Range("G1:L1").Cells(1, x).EntireColumn.Delete
        Next x
    
    End Sub
    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    10-26-2011
    Location
    York
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Delete entire column if all cells in a certain range are empty

    Thanks Dom, that works nicely

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete entire column if all cells in a certain range are empty

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Create a helper row 1 and use =IF(SUM(C4:C1001)=0,"delete","keep")

    Now use a macro along the lines of
    Sub DeleteColumns()
        Dim x As Long
        For x = Range("G1:L1").Cells.Count To 1 Step -1
            If LCase(Range("G1:L1").Cells(1, x)) = "delete" Then Range("G1:L1").Cells(1, x).EntireColumn.Delete
        Next x
    
    End Sub
    Regards
    That formula should of course be put in C1 and copied across to FZ1 and the Range in the macro refer to C1:FZ1 and not my test range of G1:L1

    Regards

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Delete entire column if all cells in a certain range are empty

    another option:
    Sub Andula() 
    Dim i&, r As Range, rDel As Range
    
    For i = 3 To 182    'columns C...FZ
        Set r = Cells(3, i).Resize(998)
        If r.Text = "" Then 'Note r.value=0 <> r.value=""
            If rDel Is Nothing Then Set rDel = Cells(3, i) Else Set rDel = Union(rDel, Cells(3, i))
        End If
    Next i
    If Not rDel Is Nothing Then rDel.EntireColumn.Delete
    End Sub

  7. #7
    Registered User
    Join Date
    10-26-2011
    Location
    York
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Delete entire column if all cells in a certain range are empty

    Nilem - even better, thanks

+ 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