+ Reply to Thread
Results 1 to 8 of 8

Remove blank columns

  1. #1
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    41

    Remove blank columns

    I have a spreadsheet with app. 20,000 rows and 102 (CX) columns. I need help writing a macro that will start at column A and check to see if all the cells in that column starting at row 2 are empty. If all cells are empty I would like to delete the column.

    The issue I'm having wrapping my head around this is that if I delete column A and everything shifts to the left and I now go to column 2 I am missing one column that may need to be deleted.

    The first row is field names and all 102 have data in them. I would like to start at row 2.

    I am using excel 2007.

    Thanks
    Last edited by extrapulp; 04-26-2010 at 03:09 PM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Remove blank columns

    Hi, Try this , this is one way !!!

    Please Login or Register  to view this content.
    Regards Mick

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Remove blank columns

    Two suggestions:

    1) Set your loop to run backwards so it is starting with the last column and moving to the left. This way as columns are deleted it has no effect on the count of the position you're in as you continue.

    2) Do not delete anything until the end - instead set a range as you go memorizing the columns to delete, then delete them all at once at the end. This is how I would do it. (Using Mick's suggested column evaluation technique)

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 04-26-2010 at 11:50 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    41

    Re: Remove blank columns

    MickG and JBeaucaire,

    JBeaucaire's code runs right until it tries to do the delete. This is because it is looking at row 1 also. This row has data in it. I get a 'no cells selected' runtime error when it hits the deletion line.
    Now I'm no expert here but could we cut the first row out and then insert it back in when the deletion was done? Or would that be an overly complicated way to try it?


    Thanks for the quick replies!

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Remove blank columns

    Row 1 is not causing the problem, you're misreading what the macro is trying to do as it evaluates each column. Anyway, I didn't test Mick's evaluation technique since he's pretty sharp, so the error could be there or something else I can't picture at the moment.

    Post up a sample workbook with the macro installed and let me take a look.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.

  6. #6
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    41

    Re: Remove blank columns

    OK attached is a sampling of the data (just not all the rows) and the macro is in it.

    Thanks!
    Attached Files Attached Files

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Remove blank columns

    It appears Mick's evaluation technique wasn't working properly for your data, so here's the macro with my standard technique instead, it will delete any column that has nothing in it after row 1:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    41

    Re: Remove blank columns

    JBeaucire,

    That worked wonderfully!

    Thank you very much for the code, and the super quick replies!

+ 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