+ Reply to Thread
Results 1 to 13 of 13

Delete all columns if value in row 2 of that column is x?

  1. #1
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Delete all columns if value in row 2 of that column is x?

    Hi Guys,

    I'm looking for the correct way of deleting coulmns based on if row 2 has an x in it..

    I have two versions that I tried but im pretty sure there are faster ways of doing it, I don't quite know how to delete all the columns at once.


    Please Login or Register  to view this content.
    The first version doesn't work for some reason and the second column works but is a slow loop, any ideas what to do to make this faster?

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Delete all columns if value in row 2 of that column is x?

    Try this:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Delete all columns if value in row 2 of that column is x?

    Try this..

    Please Login or Register  to view this content.
    Don't forget to click *

  4. #4
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Delete all columns if value in row 2 of that column is x?

    I just made these two trying to use the union function but neither work

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    However neither work correctly

  5. #5
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Delete all columns if value in row 2 of that column is x?

    Quote Originally Posted by mehmetcik View Post
    Try this...

    snip
    I modified yours a bit and I dont quite know how to get it working for the specific worksheet that it's on...

    Please Login or Register  to view this content.
    It seems to do what I want it to do but I surely should be able to do this without having to use Worksheet.Activate?

  6. #6
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Delete all columns if value in row 2 of that column is x?

    what about mine..?

    Vikas Gautam

  7. #7
    Forum Contributor
    Join Date
    09-07-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    358

    Re: Delete all columns if value in row 2 of that column is x?

    Quote Originally Posted by Vikas_Gautam View Post
    Try this..

    Please Login or Register  to view this content.
    Don't forget to click *
    Quote Originally Posted by Vikas_Gautam View Post
    what about mine..?

    Vikas Gautam

    Your code doesn't work, the loop is wrong the way I fixed your loop is by doing:

    Please Login or Register  to view this content.
    However even then it still doesn't work because it has a problem I avoided in my first post by stepping backwards from far right to the left... basically if you had B2 and C2 with an x in each cell then when it deletes column B it will make C shift leftwards and then when it cycles it skips and misses C

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Delete all columns if value in row 2 of that column is x?

    Quote Originally Posted by Vikas_Gautam View Post
    Please Login or Register  to view this content.
    Quote Originally Posted by Vikas_Gautam View Post
    what about mine..?
    If you have tried your code yourself, you will find that it doesn't work.
    多么想要告诉你 我好喜欢你

  9. #9
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Delete all columns if value in row 2 of that column is x?

    try
    Please Login or Register  to view this content.

  10. #10
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Delete all columns if value in row 2 of that column is x?

    Try this..then..

    Please Login or Register  to view this content.
    Don't forget to click *

  11. #11
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Delete all columns if value in row 2 of that column is x?

    or try this..
    Please Login or Register  to view this content.
    I have used Clearcontents method..

    Don't forget to click *

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete all columns if value in row 2 of that column is x?

    Hi, Vikas_Gautam,

    place an x in 2 cells like C and D and the value in C will stay after your suggestion. Deletion sould either be done from the bottom to the top (or the right to lthe left) or by using a range object and deleting the big range at the end:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    [ Edit ]And of course ClearContents will work properly while Delete won´t..[ /Edit]

    Ciao,
    Holger
    Last edited by HaHoBe; 08-14-2014 at 12:11 AM.
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  13. #13
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Delete all columns if value in row 2 of that column is x?

    Thanks for the suggetion..

    Vikas Gautam

+ 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. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  3. Need help - Delete columns based on column name
    By amitvba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2011, 04:37 PM
  4. Delete columns where the last row in the column = 0
    By Melyssa18 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2010, 01:09 AM
  5. Delete multiple columns by column name no criteria need, just delete them
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2009, 10:40 AM

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