+ Reply to Thread
Results 1 to 24 of 24

Slow Macro - Hide Row based on cell value

  1. #1
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Slow Macro - Hide Row based on cell value

    I have a macro that will hide a row based on a cell's value inside that row. The problem is, it seems to take a very long time to perform this task. I know our work machines here are slow but is there anything that can be done to the code below to speed it up?

    Screen updating and events are turned off.

    Please Login or Register  to view this content.

    "Hide_Rows_Test" =Gen_3!$CB$17:$CB$626
    Last edited by PY_; 03-04-2011 at 10:39 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Slow Macro - Hide Row based on cell value

    The problem is probably because you are looping through the range & hiding individual rows, it's generally quicker to create a range of the cells & then hide.

    What value are you looking for?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    Basically within the range: If the value = "" then hide row, If the value = "x" then keep unhidden.

  4. #4
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    This saved me 6 seconds off my time (according to a Now() calculation before and after each code). But how can i make it work with a named range so the numbers change as the range grows (when i add more to the worksheet)

    Code above = 1:38 to run
    Code below = 1:32 to run

    Please Login or Register  to view this content.
    Last edited by PY_; 03-02-2011 at 02:37 PM.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Slow Macro - Hide Row based on cell value

    If your data is in a table format then use AutoFilter, see my code here

  6. #6
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    With the way it is all set-up ,it's not in a table so unfortunaltely filtering will not work. It looks like i am stuck with the way it runs now. Shaving 6 seconds off the total time does help somewhat though. Ill just have to remember that if i add data to the worksheet, i need to adjust that number in my vba.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Slow Macro - Hide Row based on cell value

    Sounds like it would be worth the effort to design an efficient spreadsheet

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Slow Macro - Hide Row based on cell value

    hi, may be this way will help you to speed up hiding rows, please check attachment, run code "test"
    Attached Files Attached Files

  9. #9
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    I would if i could roy. The way this worksheet is layed out i am unable to do so.

    Thanks waterserv, will check it out this evening.

  10. #10
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    I am guessing it is grouping all rows with a "2" in column "A" together and then hiding those grouped rows.

    I am not familar with how it is typed so i am unable to edit it. I know this is a dumb question but: Can you explain how i change from looking in row A to looking in row C for example?

    Nevermind, i got it figured out. Now to test it.
    Last edited by PY_; 03-03-2011 at 08:02 PM.

  11. #11
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    I have attached a stripped version of my worksheet here. Watersev, i get a "Method 'Range' of object '_Global' failed" on this line:
    Please Login or Register  to view this content.
    .
    It could be because I messed up when adjusting your code but im not sure.


    Edit: Pasted the wrong worksheet + code in my post. It has been corrected.
    Attached Files Attached Files
    Last edited by PY_; 03-04-2011 at 09:51 AM.

  12. #12
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Slow Macro - Hide Row based on cell value

    hi, PY_, what rows should be hidden, marked to hide or to leave visible and hide all the rest?

  13. #13
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    Rows with "x" stay visible. Rows with blank cells, hide.

    I can change this however is best but i am only looking within a specific range.
    Last edited by PY_; 03-04-2011 at 09:23 AM.

  14. #14
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Slow Macro - Hide Row based on cell value

    please check attachment, code "watersev1"
    Attached Files Attached Files

  15. #15
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Re: Slow Macro - Hide Row based on cell value

    watersev,
    That code is truly amazing compared to the ones i had! Check out the times on an actual worksheet run:

    Hide_Accessory_Rows - took 2:48
    i_Hide - took 2:24
    watersev - took 0:02

    Thank you so much for the help!

  16. #16
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Slow Macro - Hide Row based on cell value

    you are welcome

  17. #17
    Registered User
    Join Date
    03-11-2012
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Slow Macro - Hide Row based on cell value

    Wow Watersev this macro is awesome!!!

    Can you help me with a similar situation?

    I need to hide columns instead of rows, and in my case the range is from column G to column BD.
    In the row number 6 I have numbers
    If the number in this cell is 0 the column has to be hidden

    I have tried to modify your macro, but my macro skills are not so professional.

    Thank you in advance!

    Manolo

  18. #18
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,624

    Re: Slow Macro - Hide Row based on cell value

    @poldark:
    Please read and adhere to these simple rules!

    1. ....

    2. Don't post a question in the thread of another member -- start your own. If you feel it's particularly relevant, provide a link to the other thread.
    Ben Van Johnson

  19. #19
    Registered User
    Join Date
    03-11-2012
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Slow Macro - Hide Row based on cell value

    Ok I will proceed as you indicate.

    Sorry for the error. I am new here

    Thank you very much for this excelent forum!

    Manolo

  20. #20
    Registered User
    Join Date
    07-18-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Slow Macro - Hide Row based on cell value

    Hi All,

    I have a data sheet (AGNT) for all sales made by an Agent identified by the country.

    I have made a report in the next sheet (CTRY), whereby on the country selection the values will get updated only for the agents for that particular selected country.

    I wanted to hide the cells & only populate the cells which match the selection and have values.

    How can i do this with a Macro?

    Attaching the same for your reference.
    Attached Files Attached Files

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Slow Macro - Hide Row based on cell value

    Bokishai,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  22. #22
    Registered User
    Join Date
    07-18-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Slow Macro - Hide Row based on cell value

    Can u help me posting a new thread for this question?

  23. #23
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Slow Macro - Hide Row based on cell value

    Click on the "Start New Thread" button

  24. #24
    Forum Contributor
    Join Date
    07-17-2012
    Location
    N/A
    MS-Off Ver
    Excel 2010
    Posts
    218

    Re: Slow Macro - Hide Row based on cell value

    Hi,

    When I tried to reuse the code from "TestHideRows"

    Please Login or Register  to view this content.
    I get this: error:'1004': Method 'Range' of object '_Global' failed.

    What am I doing wrong?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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