+ Reply to Thread
Results 1 to 6 of 6

Delete a column based on a heading cell value and loop

  1. #1
    Registered User
    Join Date
    07-27-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007,2010, 2013
    Posts
    26

    Delete a column based on a heading cell value and loop

    Dear all
    hope you are doing well
    I need help on a delete if code.
    There are two spreadsheets on the same workbook; sheet1 with a column of numbers 1 - 950 and sheet2 with a panel where the top row is a set of numbers to be compared to sheet1 column. the operation would be to delete column data in sheet2 if the cell on top of this column does not exist among numbers in sheet1 column.

    sheet1
    Deals to keep
    1
    2
    4
    5
    6
    Sheet 2
    SR NO 1 2 3 4 5
    -240 0.001545 0.000267 0.007754 -0.002630 -0.002630
    -239 0.003810 -0.009662 -0.009662 0.042933 0.003798
    -238 -0.001717 -0.002862 -0.002862 0.029076 0.007287
    -237 -0.013669 0.012070 0.012070 -0.001426 -0.003474

    for example here, i want excel to detect that because there is no 3 in sheet1 column to delete the content of the column headed 3 in sheet 2.
    hope that is clear enough for your kind help.

    I have the following but it is not running, it is deleting from column no. 3 instead and not the whole column;
    Sub DeleteCells2()

    Dim rng As Range
    Dim i As Integer, counter As Integer

    'Set the range to evaluate to rng.
    Set rng1 = ActiveSheet.Range("B6:AST6")
    Set rng2 = Sheets("deals filtered").Range("C2:C919")

    'initialize i to 1
    i = 1
    'Loop for a count of 1 to the number of rows
    'in the range that you want to evaluate.
    For counter = 1 To 1189

    'If cell i in the range contains an "x",
    'delete the row.
    'Else increment i
    If rng1.Cells(i) = rng2.Cells(i) Then
    rng1.Cells(i).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    Else
    i = i + 1
    End If

    Next

    End Sub

    does it run differently if it is MS2007,2010, or 2013
    Best wishes
    sheet 2.pngsheet 1.png

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,881

    Re: Delete a column based on a heading cell value and loop

    Try this macro with Sheet2 being the active sheet.
    Please Login or Register  to view this content.
    This macro will delete the entire column. If you just want to clear the cells without deleting the column, then change this line:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    Last edited by Mumps1; 07-02-2015 at 10:28 AM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    07-27-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007,2010, 2013
    Posts
    26

    Re: Delete a column based on a heading cell value and loop

    Thanks Mumps1,

    How to hide and / or delete the columns affected by the if condition only.

    Regards

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,881

    Re: Delete a column based on a heading cell value and loop

    My apologies. I'm not sure what you mean by
    How to hide and / or delete the columns affected by the if condition only.
    The macro I suggested does exactly what you describe in your original post:
    delete column data in sheet2 if the cell on top of this column does not exist among numbers in sheet1 column.

  5. #5
    Registered User
    Join Date
    07-27-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007,2010, 2013
    Posts
    26

    Re: Delete a column based on a heading cell value and loop

    It does exactly greatly many thanks.

    It is not from the original post.
    I meant if I want after deleting the entire column to make columns shift and update so that:
    If Columns B & D are subject to the if clause and to be deleted, B will become A and D will become C, if possible.

    Regards

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,881

    Re: Delete a column based on a heading cell value and loop

    When a column is deleted, all the columns to the right are shifted to the left. When you say
    B will become A
    that move will create a problem because you currently have data in column A of Sheet2. If B becomes A, the current data in column A would be overwritten.

+ 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] Delete Column if first cell does not equal correct heading
    By Justair07 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-20-2013, 09:23 AM
  2. Delete Rows Based On Cell Value (Without For-Next Loop)
    By dkcel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2013, 11:48 PM
  3. [SOLVED] Delete columns based upon a column heading?
    By Bandicoot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2013, 10:02 AM
  4. delete column heading for print
    By Daffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-10-2005, 03:40 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