+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Run macro until the end of the sheet

  1. #1
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Run macro until the end of the sheet

    I had some changes in the system, so now I need to run the macro until the end of the sheet (last row with data). Column A has data in every row.

    Sub HideRows()
        BeginRow = 1
        EndRow = 60
        ChkCol = 4
        ChkCol2 = 6
        
        Sheets("27.09.-03.10.").Select
        Cells.Select
        Cells.EntireRow.AutoFit
    
        For RowCnt = BeginRow To EndRow
            If Cells(RowCnt, ChkCol).Value = 0 And Cells(RowCnt, ChkCol2).Value = 0 Then
                Cells(RowCnt, ChkCol).EntireRow.Hidden = True
            End If
        Next RowCnt
    End Sub
    Thanks in advance.
    Last edited by evilgrin; 10-02-2010 at 04:30 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the row of the selected sheet

    Please explain exactly what you mean. A selected sheet isn't a row so your question is impossible to imagine.

    Try again, please.


    NOTE: Good job using the INDENT function to overcome the forum's current inability to let you color text when you use code tags instead. Very creative and your solution supports the "spirit" of the code tag rule which is make sure code indents itself properly to make it easy for everyone to read.

    Well done.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the row of the selected sheet

    So suppose I have selected a row - is it possible to make the macro understand the selected row as endrow?
    Last edited by evilgrin; 09-28-2010 at 10:05 AM.

  4. #4
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the row of the selected cell

    Ahh, sorry, not sheet but cell. Gotta get some more coffee. Corrected.

  5. #5
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Run macro until the row of the selected cell

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  6. #6
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the row of the selected cell

    Still learning how to do things right here. Corrected that too.

    So any advice on how to accomplish this? Another option - I could manually insert the number of the EndRow in, say, cell H1, how do I make the macro read it?

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the row of the selected cell

    In VBA you can solve problems dynamically. There's no reason you should ever have to "help" the macro find the last row with data in it. There are many, many standard methods for examining the data on the sheet and identifying the last row.

    1) Look at a specific column that always is filled in for each row of data, like column A, look up from the bottom of the worksheet and find the last cell in column A with a value and mark that as the last row
    Dim LR as Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row

    2) Examine every cell in the worksheet and find the last cell with data in it, mark that row as the last row regardless of which column it is in:
    Dim LR as Long
    
    LR = Cells.Find("*", Cells(Rows.Count, Columns.Count), _
       SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    3) Your suggestion for selected cell:
    Dim LR as Long
    
    LR = ActiveCell.Row

    4) Your suggestion for cell value:
    Dim LR as Long
    
    LR = Range("H1").Value
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  8. #8
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the row of the selected cell

    Ok, will try to explain more in detail:

    The worksheets have 5 rows for every day and almost every day some data is pasted in the rows, yet usually a few rows remain blank. I need to hide the blank rows for the previous days and leave those for the remaining days intact. That's why it's useful to run the macro until the selected row.

    I'm kinda new to VBA programming and still learning to put together very simple codes so I'd be very grateful if you could help me putting this in the code.

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the row of the selected cell

    Your issue is no longer clear. Please provide a sample workbook demonstrating completely the scenario. Use BEFORE/AFTER sheets if necessary to make it clear.

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  10. #10
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the end of the sheet

    Here's a sample sheet.
    Seems that the following code would work but I don't know how to insert it in the macro.
    Dim LR as Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row

    Perhaps something like this?
    Sub HideRows()
    Dim LR as Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
        ChkCol = 4
        ChkCol2 = 6
        
        Sheets("27.09.-03.10.").Select
        Cells.Select
        Cells.EntireRow.AutoFit
    
        For LR
            If Cells(RowCnt, ChkCol).Value = 0 And Cells(RowCnt, ChkCol2).Value = 0 Then
                Cells(RowCnt, ChkCol).EntireRow.Hidden = True
            End If
        Next RowCnt
    End Sub
    Attached Files Attached Files
    Last edited by evilgrin; 10-01-2010 at 06:45 AM.

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the end of the sheet

    As I said before, there are many ways to determine the last row of data, and yours is even more specific because you only "need" rows up through yesterday, right?

    So for your macro, I would search for today's date and use that row as the basis for stopping, leaving all rows after that visible.

    Then instead of looping through the rows one at a time, you can instantly make a RANGE using SpecialCells of all the blank cells in column D down through your last row, then do the same thing with column E, then find where there two ranges intersect (meaning blank in both D and E), and then hide the rows all at once with a single command.

    This is how I would write your routine:
    Option Explicit
    
    Sub HideRows()
    Dim LR As Long, RNG As Range
    Dim RNG2 As Range, RNG3 As Range
    Application.ScreenUpdating = False
    
    'Find today's date on sheet and use as last row
        LR = Cells.Find(What:=Format(Date, "d-mmm-yy"), After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=True).Row
    
    'Make a range of all blank rows in column D
        Set RNG = Range("D3:D" & LR).SpecialCells(xlCellTypeBlanks).EntireRow
    'Make a range of all blank rows in column E
        Set RNG2 = Range("E3:E" & LR).SpecialCells(xlCellTypeBlanks).EntireRow
    'Make a range of where RNG and RNG2 share rows
        Set RNG3 = Intersect(RNG, RNG2)
    
    'Hide all rows in RNG3
        RNG3.EntireRow.Hidden = True
    
    'Cleanup
        Set RNG = Nothing
        Set RNG2 = Nothing
        Set RNG3 = Nothing
        Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the end of the sheet

    Of course the same thing can be shortened up but the resulting code is much less clearer what's going on, but this is effectively the same thing:
    Option Explicit
    
    Sub HideRows()
    Dim LR As Long
    Application.ScreenUpdating = False
    
    'Find today's date on sheet and use as last row
        LR = Cells.Find(What:=Format(Date, "d-mmm-yy"), After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=True).Row
    
    'Hide all rows blank in column D and E
        Intersect(Range("D3:D" & LR).SpecialCells(xlCellTypeBlanks).EntireRow, _
            Range("E3:E" & LR).SpecialCells(xlCellTypeBlanks).EntireRow).EntireRow.Hidden = True
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  13. #13
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the end of the sheet

    Wow thanks, this is much better than the original code. One problem though - data for today will be added either today afternoon or tomorrow so how to make the code hide rows until yesterday, not today? Oh, and I also need rows with 0s in columns D and E hidden.
    Last edited by evilgrin; 10-01-2010 at 10:46 AM.

  14. #14
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Run macro until the end of the sheet

    The code provided above clearly shows where the "find today's date" code is. Just subtract 1 from the date.

    'Find today's date on sheet and use as last row
        LR = Cells.Find(What:=Format(Date - 1, "d-mmm-yy"), After:=ActiveCell, LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=True).Row

    Too bad about the zeros thing, too, was hoping you could avoid looping row by row based on your sample sheet.

    NOTE: Sample sheets should always fully represent your need, all variations. By dumbing it down even a little people end up wasting a lot of time on solutions/methods that won't pan out in the end.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  15. #15
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Run macro until the end of the sheet

    I never thought it would be so easy. I tried with -1, just didn't put it in the right place.
    All right then, I'll just make this macro run whenever the sheet is opened and run the loop at the end of the week. Thanks again, I really appreciate your help!!!

+ 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.2.0