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.
Thanks in advance.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![]()
Last edited by evilgrin; 10-02-2010 at 04:30 AM.
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 theicon 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!)
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.
Ahh, sorry, not sheet but cell. Gotta get some more coffee. Corrected.
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)
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?
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 theicon 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!)
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.
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 theicon 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!)
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
Last edited by evilgrin; 10-01-2010 at 06:45 AM.
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 theicon 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!)
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 theicon 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!)
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.
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 theicon 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!)
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!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks