+ Reply to Thread
Results 1 to 7 of 7

Thread: Auto delete columns

  1. #1
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    2003
    Posts
    191

    Auto delete columns

    Hi

    I currently use a spreadsheet to record weekly vehicle mileage. I'm looking for a way to automatically delete a column based on a cell value within the column when the vehicle goes off strength
    Last edited by JamesT1; 05-04-2009 at 02:17 PM. Reason: more information

  2. #2
    Valued Forum Contributor
    Join Date
    04-18-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    256

    Re: Auto delete columns

    Hi James,

    Can you attach a sample workbook to make us understand your problem better?

    --Karan--

  3. #3
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    2003
    Posts
    191

    Re: Auto delete columns

    Hi Karen

    The spreadsheet is too large to attach,,,,, the attachment has a small example.....

    If the vehicle in cell "G2" is taken off strength and this cell is blank then I am looking for column G to be automatically deleted

    Thanks
    Attached Files Attached Files

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Auto delete columns

    What does "taken off strength" mean to us? And what do you mean by "automatically"? That's a very iffy concept.

    Click a button and the columns "automatically" delete if appropriate
    Change a value in a cell and the column "automatically deletes (crazy thought, IMO.)
    _________________
    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!)

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Auto delete columns

    Going by the key phrase "if G2 is blank" I wrote a macro that will delete any column that has blank cell in row2 starting at cell C2.

    Your sample sheet had formulas, so when I deleted G2 to make it empty and ran the macro, it did what you wanted, but the formulas in the proceeding columns are broken by that process. FYI.
    Option Explicit
    
    Sub OffStrength()
    Dim Rng As Range, cell As Range, LastCol As Long
    LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
    Set Rng = Range("C2", Cells(2, LastCol))
    
        For Each cell In Rng
            If IsEmpty(cell) Then Columns(cell.Column).Delete shift:=xlToLeft
        Next cell
        
    Set Rng = Nothing
    End Sub
    The on-demand macro is my suggestion, if it were "automatic"...what if you made a mistake? Oops.
    _________________
    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!)

  6. #6
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    2003
    Posts
    191

    Re: Auto delete columns

    Hi JBeaucaire

    "Taken Off Strength" means the vehicle has been sold or disposed off,,,,, when this happens I'm looking to delete the column where the weekly miles are kept,........

  7. #7
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    2003
    Posts
    191

    Re: Auto delete columns

    Hi JBeaucarie

    My Apologies,,,,, in a hurray to put the example together and copied the formulas left to right,,, they should be vertical,,,,

    many thanks for the code I will try it on my sheet and let you know how I get on

+ 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