+ Reply to Thread
Results 1 to 14 of 14

More efficient way to find and delete columns

  1. #1
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    More efficient way to find and delete columns

    Good morning folks,
    With lots of help from browsing this forum and asking a few questions I have finally gotten my large macro to work. I have broken it down into pieces and I have a master macro to call each piece. This makes it easier to tweek the behaviour.
    Anyway, one of my subroutines searches row one for certain column headers and deletes the columns that don't contain my search criteria.
    The thing is there are about one hundred columns and this part of the macro takes just under 4 minutes to run.
    Can anyone suggest a way of making it run more efficiently please.

    Here is the code. My search criteria usually appear roughly in order so I am going to start by reversing the order in which they are listed in my code.

    Please Login or Register  to view this content.

    I have actual search items for privacy reasons.

    I would appreciate anyones input that could help reduce the runtime of the above routine which as I said is just under 4 minutes.
    Thanks in advance.

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

    Re: More efficient way to find and delete columns

    Loop back word

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    Thanks so much for that.
    I've gone from 3:54 to 1:46 which is excellent.
    Since it is now counting backwards, would reversing the order of my search strings have any impact??
    Anyone else have any ideas to improve on 1:46???
    Thanks again.
    I love this forum.

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

    Re: More efficient way to find and delete columns

    Since it is now counting backwards, would reversing the order of my search strings have any impact??
    No!
    Try this one and will be away until this afternoon.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    Hi,
    I'm getting a runtime error 424 on that one.
    Object not defined.
    It appears that myrng1 isn't defined properly?? So I did the following
    Please Login or Register  to view this content.
    And now I'm getting runtime erro 13 type mismatch on the Union.... bit

    Any idea. If you're gone already, I thank you for your help so far.
    Last edited by agentblue; 04-30-2013 at 10:40 AM.

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

    Re: More efficient way to find and delete columns

    Try these lines

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    No Joy
    I'm using this now
    Please Login or Register  to view this content.
    And I get Runtime Error 1004 Range Class failed on the .EntireColumn.Delete line.
    Sorry for the trouble.
    If I don't include the Set myrng1 piece at the start it throws up the object not defined error.
    Last edited by agentblue; 04-30-2013 at 10:41 AM.

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

    Re: More efficient way to find and delete columns

    Remove "1" from 1

  9. #9
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    Thanks AB33 but still no joy
    I changed it to

    Please Login or Register  to view this content.
    But it still hangs on the EntireColumn.Delete line with Delete Methon Of Range Class failed
    Any suggestions. Thanks very much for your patience.

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

    Re: More efficient way to find and delete columns

    We need to find another way round.

    If InStr("Text1 Text2 text3 Text4 Text5 Text6 Text7 Text8 Text9 Text10", Cells(1, i).Text) = 0 Then
    Do you want to delete a column if a cell has these texts, or the other way round?

  11. #11
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    I want to delete the column if a cell DOES NOT have those texts.
    So for example, I have columns with headers saying text1 text 2.... text 100
    i want to use the above macro with text1 text 5 text 7 text11 etc, so that I keep just those columns and delete the rest.
    If this is too much work, don't worry as you have already reduced the run time from 3:54 to 1:46 with your first suggestion. Thank you.

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

    Re: More efficient way to find and delete columns

    Try this one. I did not get an error, but not sure if it works

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    04-04-2013
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: More efficient way to find and delete columns

    My god man,
    You are a genius. Thank you so much. That runs now in 5 seconds!!! I am amazed.
    The very last thing I have to do to finish this project is to perform a frequency analysis on my results, and I'm having trouble with the array formula in VBA but that is a question for tomorrow.
    Thank you so much for your help.
    Regards.

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

    Re: More efficient way to find and delete columns

    Agentblue,
    Glad it has worked for you! Your patience has paid off
    You are welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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