+ Reply to Thread
Results 1 to 14 of 14

Delete Column only after certain Row with Specified Header

  1. #1
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Delete Column only after certain Row with Specified Header

    Hi,

    I am looking to delete the entire row of a data from row 9 and below while keeping the format of the boxes I have above row 9. Right now after my macro runs, the boxes shrink as well because columns have been deleted.

    Also, I'm not sure why my macro with specified headers is not deleting some of the headers that is not from the list.

    I took the macro from this thread here and made some minor tweaks to it on my own: http://www.excelforum.com/excel-prog...d-headers.html

    Please see my attached file for a more clear description.

    Any help would be greatly appreciated!

    Thanks!

    DelColumnHeader.xlsm

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Delete Column only after certain Row with Specified Header

    Your example workbook doesn't have any code in it.

    Instead of deleting (.Delete) rows or columns, use .ClearContents instead. That will remove the values and keep the formatting.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Hi,

    Here is the formula I used in the workbook:

    Sub DelColumnNotInList()
    Dim LastCol As Double
    Dim ColCtr As Double
    LastCol = Cells(9, Columns.Count).End(xlToLeft).Column
    For ColCtr = 1 To LastCol
    If InStr("Name Title Age Position ID", Cells(9, ColCtr).Text) = 0 Then
    Cells(1, ColCtr).EntireColumn.Delete
    ColCtr = ColCtr - 1
    End If
    Next ColCtr
    End Sub


    I tried using clear-contents, but I would actually like the columns removed and to have those columns shifted as it would if the column was deleted, but just that the Box Shape I have would also delete as well.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete Column only after certain Row with Specified Header

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Quote Originally Posted by AB33 View Post
    Please Login or Register  to view this content.
    Thanks! This is exactly what I needed!

    When I run it though, the Headers with "A" and "D" are still there, even though I know the macro doesn't have it listed as one of the headers.

    Do you know how that could be fixed?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Delete Column only after certain Row with Specified Header

    Quote Originally Posted by Pho6 View Post
    I would actually like the columns removed and to have those columns shifted as it would if the column was deleted, but just that the Box Shape I have would also delete as well.
    • Right-click on each box shape and select Format AutoShape from the pop-up context menu.
    • Select the Properties tab on the Format AutoShape dialog.
    • Select the "Don't move or size with cells" option and OK.

  7. #7
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Quote Originally Posted by AlphaFrog View Post
    • Right-click on each box shape and select Format AutoShape from the pop-up context menu.
    • Select the Properties tab on the Format AutoShape dialog.
    • Select the "Don't move or size with cells" option and OK.
    Thanks! Had no idea you could do that... So simple!

    From my code I pasted above, do you know why my headers "A" and "D" are not deleting?

  8. #8
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Quote Originally Posted by AlphaFrog View Post
    • Right-click on each box shape and select Format AutoShape from the pop-up context menu.
    • Select the Properties tab on the Format AutoShape dialog.
    • Select the "Don't move or size with cells" option and OK.
    Thanks! Had no idea you could do that... So simple!

    From my code I pasted above, do you know why my headers "A" and "D" are not deleting?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete Column only after certain Row with Specified Header

    Because you are deleting an entire column and as the result the columns shift.

  10. #10
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Is there a way to make it work so that only the headers:

    "Name Title Age Position ID" stay despite the result of columns shifting?

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete Column only after certain Row with Specified Header

    Pho6,
    Please do not reply with quote, just a reply will do.
    I am not sure I understand your request. How different from what you now have?

  12. #12
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    Sorry about that.

    Basically, when I run the code it currently gives me a result of:

    Name A Title Age Position D ID

    Even though the code: "If InStr("Name Title Age Position ID", Cells(9, j).Text)" does not include "A" or "D" in the string. Is this occuring because of the columns shifting? Is there anyway to change it so after the macro runs, it only gives me:

    Name Title Age Position ID

    Thanks

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Delete Column only after certain Row with Specified Header

    InStr function match any word with in a text. Age match A and ID match D

  14. #14
    Registered User
    Join Date
    07-11-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Delete Column only after certain Row with Specified Header

    I see. Got it now. Thanks!

+ 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. If column contains certain header, delete column--exempt certain columns
    By kestefon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2013, 04:04 PM
  2. Delete column range based on header text
    By sunsoar77 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-22-2012, 07:45 PM
  3. Delete column based on header row value
    By dagindi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-31-2011, 02:30 PM
  4. Delete column based on header row value
    By Spider_dude in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2009, 03:01 PM
  5. Delete Column based on Column header
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-01-2009, 08:57 AM

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