+ Reply to Thread
Results 1 to 10 of 10

Replace Macro recorded code with more effective code

  1. #1
    Valued Forum Contributor scaffdog845's Avatar
    Join Date
    02-01-2008
    Location
    Aston, PA. USA.
    MS-Off Ver
    Microsoft 365
    Posts
    375

    Replace Macro recorded code with more effective code

    Hey

    I have the following code that was recorded through the Macro record function. The goal was to exclude the cells that had the word "PRE" in it. At the time I recorded the Macro there was only a few things in the list other than "PRE". If someone adds a value not in the code and the Macro is run, the new value is also excluded from the filtered list.

    How can I filter $A$1:$P$25001 to exclude only values of "PRE" contained in E:E ?

    Here is the code from the Macro

    Please Login or Register  to view this content.
    Click here to read the Forum Rules
    Whatever it is in life you decide to go after, go after with great ferocity.

  2. #2
    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: Replace Macro recorded code with more effective code

    Hi, scaffdog845,

    AFAIR no direct way to do so but you may consider using either building up a Dictionary of unique items, a list in another sheet to refer to or a helper column to filter by.

    Ciao,
    Holger
    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

  3. #3
    Valued Forum Contributor scaffdog845's Avatar
    Join Date
    02-01-2008
    Location
    Aston, PA. USA.
    MS-Off Ver
    Microsoft 365
    Posts
    375

    Re: Replace Macro recorded code with more effective code

    Thanks Holger. I thought just about anything was feasible with VBA and here I though this request would be an especially easy one for the VBA gurus out there.

  4. #4
    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: Replace Macro recorded code with more effective code

    Hi, scaffdog845,

    have you had a look at the Advanced Filter which will perform as you require (sorry I donīt know a way to exclude terms with AutoFilter except what I indicated).

    BTW: could you please update your signature - the scales are gone.

    Ciao,
    Holger

  5. #5
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Replace Macro recorded code with more effective code

    scaffdog, you could try something like this. Basically you are adding a column to the right of your data, I usually call it "Exclude", then the macro puts a 1 on each row you want to ignore based on an array of values you supply. In this case, if there are any cells in E:E = "PRE", they will be filtered out. I don't know if you want to look for any instances, i.e. you would filter out a value of "PREVIOUS", because it contains "PRE", or just keep it at exact match. Either way it would be easy to tweak. Now, this is very much a "brute force" method, so it may not be the most efficient, but it works.

    Please Login or Register  to view this content.
    Greg
    Just a guy trying to make work stuff easier.

  6. #6
    Forum Contributor
    Join Date
    03-14-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    491

    Re: Replace Macro recorded code with more effective code

    Is the ws in this code referring to worksheets?

    Please Login or Register  to view this content.
    ???

  7. #7
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Replace Macro recorded code with more effective code

    How can I filter $A$1:$P$25001 to exclude only values of "PRE" contained in E:E ?
    I just wonder: does this

    Please Login or Register  to view this content.
    solve the problem?

  8. #8
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Replace Macro recorded code with more effective code

    Quote Originally Posted by Sbarro79 View Post
    I just wonder: does this

    Please Login or Register  to view this content.
    solve the problem?
    Indeed it does work. I only offered the solution I did because it sounded like the conditions were going to grow. I had to import some messy data from a materials database one time, and I literally had to filter out hundreds of values. The only way I could come up with doing it was as I proposed above. However, instead of an array, I simply put my filter out words in another sheet. Anyhow, I think the OP should be okay now.

  9. #9
    Valued Forum Contributor scaffdog845's Avatar
    Join Date
    02-01-2008
    Location
    Aston, PA. USA.
    MS-Off Ver
    Microsoft 365
    Posts
    375

    Re: Replace Macro recorded code with more effective code

    Hey gang. Thanks for the addition comebacks. I thought I was dead in the water after seeing HaHoBe's response. Fortunately I was able to use a helper column that looked for the value "PRE" and if it existed showed the value defaulted to "HIDE" and if it didn't it defaulted to "SHOW" Pretty simple sort after that.

    I'll definitely try the additional suggestions as a learning opportunity.

    Take care

  10. #10
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Replace Macro recorded code with more effective code

    Quote Originally Posted by swade730 View Post
    Is the ws in this code referring to worksheets?

    Please Login or Register  to view this content.
    ???
    Yes, and it was referenced in the line above. Probably not necessary here, because it did not save too much typing, but I usually set the reference at the beginning because I may need to reference it later. Just my preference, but not always necessary.

    Greg

+ 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. clean code from recorded macro
    By tim5 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-07-2012, 12:45 PM
  2. Rewriting macro recorded code for efficiency
    By jhizon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2011, 01:53 PM
  3. Mod macro recorded code
    By usr789 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2007, 09:27 AM
  4. VB code request for a Working Recorded Macro
    By hsmeet in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-30-2007, 11:40 AM
  5. [SOLVED] Call recorded macro from VBA code
    By keyser_Soze@usa.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-02-2005, 05:05 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