+ Reply to Thread
Results 1 to 15 of 15

Delete multiple columns in a Table

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Delete multiple columns in a Table

    This code works to delete multiple contiguous columns in a DataRange or in a Table.
    Please Login or Register  to view this content.
    This code works to delete multiple discontiguous columns in a DataRange, but does NOT work on my Table (if I convert it to a range it works fine):
    Please Login or Register  to view this content.
    How do I need to change the second code to delete multiple discontiguous columns in a Table? The actual columns I want to delete are as shown below, and BA is the last column of my table:
    Please Login or Register  to view this content.
    Last edited by jomili; 05-26-2016 at 11:38 AM.

  2. #2
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Delete multiple columns in a Table

    try this

    Please Login or Register  to view this content.
    Last edited by biznez; 05-26-2016 at 12:09 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    BizNex,
    Result is Error code 1004, Delete method of Range class failed.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Delete multiple columns in a Table

    Try:

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    Nope, same error.

  6. #6
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: Delete multiple columns in a Table

    Hello Jomili,

    The third code in your first post should work. Remove the "Shift:=xlToLeft" part as the default will move all columns to the left anyway.

    Cheerio,
    vcoolio.

  7. #7
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    vcoolio,
    My third code is the same as John H. Davis last post. Even though I agree it "should" work, it doesn't work if my range is a Table. It DOES work if my range is simply a range.

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Delete multiple columns in a Table

    You'll need to loop:

    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    Rorya,

    Your code DID delete columns, but it didn't delete the columns I wanted. it deleted columns A, E, G, J, L, N, P, U, W, Y, AA, AD, AF, AH, AL, AN, AP, AR, AU, AW, AY and BA

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Delete multiple columns in a Table

    Apologies - you need to loop backwards, and you can do areas rather than one column at a time:
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    Okay, it works (THANK YOU!), but it raises a lot of questions for me:

    What is VBA's idea of an "Area" in this context? Would it be each section of columns in the Range?
    So would the "Areas.Count" value be 7 since there are 7 sections of columns to delete?
    And so n would be 7, then 6, then 5 and so on, down to 1?

    So in the deletion line would it delete AU:BA first, as it's the 7th "Area", then AJ:AR, etc?

  12. #12
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Delete multiple columns in a Table

    Correct. An area is a contiguous range of cells.

  13. #13
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Excel VBA Areas Collection Object Thingy :)

    As Rory said, in Excel, an Area is a contiguous range of cells . This can be "got at" in VBA throught the Areas Collection Object.

    A Range Object is more often than not a contiguous range of cells ( so that is a typical VBA guess as its "Implicit default" thingy ** ). But it does not have to be. It is not defined as such; A Range Object can be a non contiguous range of cells:

    A Range Object is a group of cells in a worksheet. The list of the cells of the range is organised in rectangular groups of cells, each one named an Area. ( Can be reffered to by its Item Number, counting, I believe in the order they were added )

    I believe when you do something like this ( Where rng is a Range Object )

    ______rng._____ =

    Then that Implicitly defaults to the first Area**

    ______rng. Areas.Item(1).______ =
    _ .. try it .. just add that bit in any code you have, - it does no harm ( well might **** some people off )

    You can build up the Areas using the Union Method

    Somethings will not "appear" to "work" for you on a Range built with the Union. Or they may only appear to "work" on the first Area you added. That is usually because the code line you write defaults to the first Area, so just applies to that first Area.
    I think you usually need to loop through to apply things to all Areas. For example this sort of thing
    ___For Each rgCell In rgUnion
    ___ _........_...
    ___ _...
    ___Next
    is the quickest I believe.

    But I think some things will work in "one go" on the Unioned Range Object, as an example:

    rgUnion.Interior.Color = vbRed



    Rem Ref
    http://www.eileenslounge.com/viewtop...181736#p181655

    Post #27 http://www.mrexcel.com/forum/excel-q...-column-3.html




    Bill Gelatine
    Last edited by Doc.AElstein; 05-28-2016 at 08:05 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Delete multiple columns in a Table

    Thanks Doc. Your explanation and links really help explain it well. And thank you Rorya for your perseverance and help in resolving this current issue. Thanks to both of you I've not only solved my issue but have learned useful information for my future projects. Thanks!

  15. #15
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    VBA Range Object. VBA Areas. Areas. Collection of Areas of contiguous cells. Wonk

    Hi jomili
    Quote Originally Posted by jomili View Post
    ....Thanks to both of you I've not only solved my issue but have learned useful information for my future projects. .....
    You is welcome. Glad we could help.
    Thanks for the Feedback
    Alan

    EDIT: Apparantly in VBA Help a range can be a 3-D range also, ????
    The definition of the Range object in VBA help states that it "represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range".
    Someons that knoes a lot about VBA Theory gave some good basic vBA info here:
    http://www.mrexcel.com/forum/excel-q...cations-2.html
    Last edited by Doc.AElstein; 06-03-2016 at 05:28 AM.

+ 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] VBA Delete Row If Ciriteria From Multiple Columns are Met
    By hobbiton73 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2014, 02:07 AM
  2. Add and Delete table columns without cells shifting?
    By brucemc777 in forum Excel General
    Replies: 3
    Last Post: 05-19-2014, 05:50 PM
  3. [SOLVED] Convert table with multiple header columns into a pivotable table
    By jasonleewkd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2013, 03:20 AM
  4. Replies: 9
    Last Post: 12-20-2012, 10:29 AM
  5. [SOLVED] Delete multiple empty cells in multiple columns and moving data up, witout Macros
    By CoraF in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-30-2012, 08:23 AM
  6. 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
  7. Delete multiple columns if they fit different criteria
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 06-23-2009, 02:36 PM

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