+ Reply to Thread
Results 1 to 7 of 7

Thread: Delete all blank columns

  1. #1
    Registered User
    Join Date
    01-12-2012
    Location
    Aylesbury
    MS-Off Ver
    Excel 2010
    Posts
    37

    Delete all blank columns

    Hi all,

    I was hoping to find an answer about deleting all of the blank columns in a range by clicking a button.

    i have tried firefly's code on another thread:
    Sub Del_Col()
    Dim i As Long
    For i = Cells(1,Columns.Count).End(xlToLeft).Column to 1 Step -1
      If Cells(1,i).EntireColumn.Find(What:="*",LookAt:=xlWhole, Searchdirection:=xlPrevious, SearchOrder:=xlByRows).Row=1 Then _
        Cells(1,i).EntireColumn.Delete
    Next i
    End Sub
    But didn't have any luck is that the right kinda code or not?

    Thanks,
    Storm08
    Last edited by Storm08; 01-24-2012 at 08:34 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    433

    Re: Delete all blank columns

    Try This:
    Sub DelUnusedCols()
    Dim i As Long, c As Long, r As Long, n As Long
    Dim oSht As Worksheet
    Dim bDel As Boolean
    Set oSht = ThisWorkbook.ActiveSheet
    c = oSht.Cells.SpecialCells(xlCellTypeLastCell).Column
    r = oSht.Cells.SpecialCells(xlCellTypeLastCell).Row
    For i = c To 1 Step -1
        bDel = True
        For n = 1 To r
            If Cells(n, i) <> "" Then
                bDel = False
                Exit For
            End If
        Next
        If bDel = True Then oSht.Columns(i).EntireColumn.Delete
    Next
    End Sub
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Registered User
    Join Date
    01-12-2012
    Location
    Aylesbury
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Delete all blank columns

    awesome, cheers! works great also, would there be a function to undo macros? e.g - so the user could undo the blank column they have just deleted?

    Ill change the name f the thread so it's more helpful to others if there's an answer?

    Thank you!

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Delete all blank columns

    sub snb()
      for each clm in usedrange.columns
        if clm.specialcells(4).count=clm.cells.count then clm.delete
      next
    next



  5. #5
    Registered User
    Join Date
    01-12-2012
    Location
    Aylesbury
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Delete all blank columns

    Awesome, cheers snb, you've been a big help!

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    433

    Re: Delete all blank columns

    Storm, Please mark the thread as SOLVED
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  7. #7
    Registered User
    Join Date
    01-12-2012
    Location
    Aylesbury
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Delete all blank columns

    Sorry! still getting used to the forum rules

+ 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.2.0