+ Reply to Thread
Results 1 to 7 of 7

How To Use UNION and Ranges To Speed Up Deleting Many Columns?

  1. #1
    Registered User
    Join Date
    11-05-2015
    Location
    San Diego, CA
    MS-Off Ver
    Professional Plus 2010
    Posts
    18

    Question How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    Trying to use Union and ranges to speed up deleting empty columns, but going wrong somewhere with the code.

    I read here that deleting columns individually slows down performance significantly. Script speed can be improved by defining a "master" range to include all the ranges (columns) to be deleted (by using Union), and then simply deleting the "master" range.

    My old (slow) script that works, but takes about about 3 hours to run across ~30 sheets and deleting ~100 columns each sheet. Using Union is supposed to make the process run in seconds instead of hours, was hoping someone could help me figure out what I'm doing wrong with my code. When I run it, nothing happens... not sure what's going on.

    Any help would be greatly appreciated!

    Please Login or Register  to view this content.

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

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    I think you're just missing a couple of S's in .Columns()

    Please Login or Register  to view this content.
    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
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    Errors corrected (in red)
    Please Login or Register  to view this content.
    Better?

    Oh, and a hint for fault finding - disable your error handling and step through the code line by line (press F8 in the VBE) to see where it fails...
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    You say there's 30 sheets yet your code is specifically dealing with a tab called Ball Shaker.

    Try this where I've added some comments:

    Please Login or Register  to view this content.
    I hope this helps!!

    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Registered User
    Join Date
    11-05-2015
    Location
    San Diego, CA
    MS-Off Ver
    Professional Plus 2010
    Posts
    18

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    Quote Originally Posted by AlphaFrog View Post
    I think you're just missing a couple of S's in .Columns()
    Thank you, totally missed that!!

    Quote Originally Posted by Olly View Post
    Errors corrected (in red)

    Better?

    Oh, and a hint for fault finding - disable your error handling and step through the code line by line (press F8 in the VBE) to see where it fails...
    Thanks m8t! Totally missed the 's' in Columns. - I'll start using the F8 technique as well, didn't think about that

    Quote Originally Posted by Trebor76 View Post
    You say there's 30 sheets yet your code is specifically dealing with a tab called Ball Shaker.

    Try this where I've added some comments:

    Please Login or Register  to view this content.
    I hope this helps!!

    Regards,

    Robert
    Thank you Robert! Is there any advantage to using h = wsMySheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    over h = wsMySheet.Range("E1").End(xlToRight).Column ?

    -----------------------

    Thank you everyone for the replies! I've used your suggestions to fix the errors in the code and have it run across multiple sheets (except "AA" and "Word Frequency"). It runs!... though I'm still new to VBA and pretty much macguyvered this code, including some statements to ignore errors... hope I'm not setting myself up for any bugs anywhere.


    Working code (across many sheets. Runs in ~6 min vs. hours using old method):

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    Thank you Robert! Is there any advantage to using h = wsMySheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    over h = wsMySheet.Range("E1").End(xlToRight).Column ?
    Yes - say you have data in columns E, F and G then no data across columns H, I and J but then some more data in column K your initial code will set column G as the last column where mine will set column K as the last column.

    Glad to hear the processing time has been dramatically reduced

    Regards,

    Robert

  7. #7
    Registered User
    Join Date
    11-05-2015
    Location
    San Diego, CA
    MS-Off Ver
    Professional Plus 2010
    Posts
    18

    Re: How To Use UNION and Ranges To Speed Up Deleting Many Columns?

    Quote Originally Posted by Trebor76 View Post
    Yes - say you have data in columns E, F and G then no data across columns H, I and J but then some more data in column K your initial code will set column G as the last column where mine will set column K as the last column.

    Glad to hear the processing time has been dramatically reduced

    Regards,

    Robert
    Ahh I see, thank you Robert! That code will definitely come in handy in the future

+ 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] Function to get Union of Ranges
    By capson in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-30-2015, 02:46 PM
  2. Using Union to combine ranges in VBA
    By sksachdev in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-27-2014, 06:59 AM
  3. Union of Named Ranges
    By justinv in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-12-2012, 12:52 PM
  4. Union Ranges Fail
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2011, 11:29 AM
  5. Union named Ranges
    By MrHockey in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-08-2009, 01:03 AM
  6. With for multiple ranges (not using Union)
    By beeawwb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2008, 09:07 PM
  7. [SOLVED] printing Union of Ranges
    By anny in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-26-2006, 06:25 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