+ Reply to Thread
Results 1 to 3 of 3

Require faster Macro Code

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    4

    Require faster Macro Code

    Hi Formum,

    I am pretty new to this Macro code stuff. I have a Macro code for a command button on the worksheet(seen below) that hide rows depending on a certain condition. The code works fine, but only if you have a couple of rows, I have 10003 rows it must look through and then hide the relevant rows. Now this will take some time to go thru all these rows and then hide the relevant ones. Is the a code that wil work faster?

    Thank you for the answer in advance.

    Sub Hide()
     Sheets("Unpaid Expenses").Select
     Range("s3:s10003").Select
     For Each Cell In Selection
     If Cell = "x" Then
     Range(Cell.Address).EntireRow.Hidden = True
     End If
     Next
     End Sub
     
    Sub Show()
     Sheets("Unpaid Expenses").Select
     Range("s3:s10003").Select
     For Each Cell In Selection
     If Cell = "x" Then
     Range(Cell.Address).EntireRow.Hidden = False
     End If
     Next
     End Sub
    Last edited by Cutter; 07-17-2012 at 01:54 PM. Reason: Added code tags

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Require faster Macro Code

    One way would be


    Sub Hide2()
    Dim rngToHide as Range, oneCell as Range
    
    With Sheets("Unpaid Expenses")
        set rngToHide = .Range("A1")
    
        For each oneCell in .Range("S3:S10003")
            If Cell.Text = "x" Then
                set rngToHide = Application.Union(rngToHide, oneCell)
            End If
        Next oneCell
        
        Application.Intersect(rngToHide, .Range("S:S").EntireRow.Hidden = True
    End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Require faster Macro Code

    @wiehan

    Hello, and welcome to the forum. Please take a look at the forum rules. You've inadvertently broken 2 of them already (#3 - code tags and #5 - duplicate posts).
    I've added the code tags for you this time but please remember to add them yourself to any code you show in your future posts. Also, I have closed the other, duplicate thread.

+ 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