Learn Excel 2010 - "Count Red Rows": Podcast #1687

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Apr 12, 2013.
Looking back to August 2011, a 'Learn Excel from MrExcel' Podcasts looked at how to Sum Colored Cells; Episode #1415 showed us how we could be obtaining the 'Sum' of the Red Rows. Today, in Episode #1687, Bill revisits that earlier Podcast to show us how to Count the Number of Rows containing Red Cells, not Sum the Cell Values.

For help adapting the code in this video to other situations (such as also making sure the cell contains "dog"), start a new topic at Excel Questions

"Learn Excel 2007 through Excel 2010 from MrExcel". Download a new two minute video every workday to learn one of the 512 Excel Mysteries Solved! and 35% More Tips than the previous edition of Bill's book! Power Excel With MrExcel - 2017 Edition

And for more on VBA in Excel 2010, check out the book, "VBA And Macros: Microsoft Excel 2010", from Bill Jelen a.k.a. MrExcel. VBA and Macros: Microsoft Excel 2010

"The Learn Excel from MrExcel Podcast Series"

www.MrExcel.com — Your One Stop for Excel Tips and Solutions. Visit us today!
maxresdefault.jpg


Transcript of the video:
MrExcel Podcast is sponsored by Easy-XL.
Learn Excel from MrExcel, Episode 1687: Count Red Rows.
Hey, welcome back to the MrExcel netcast, I'm Bill Jelen.
Yes, it's Friday. It should be a dueling Excel podcast-- it was my bad, came down here to Florida and I did not get the duel set up with Mike this week, but it'll be back next week.
So, here we're going back to Episode 1415, where I showed a little macro to count the red cells.
Actually, back then, it was sum the red cells.
But a whole discussion broke out on Youtube, someone wanted to know how to count the red cells.
Let's take a quick look at that.
I'll switch over to VBA-- Alt+F11-- and this was the tiny little Macro.
So, we have a function called CountRed, we initialize that to 0 For Each Cell In MyRange, if the color is 255-- that's the color red-- then CountRed = CountRed + 1.
Right?
Nice, simple little macro.
And then someone comes along and says, "Hey, well, no, I just want to count the rows that contain red-- if there's two red cells I want to count that as one".
And that becomes so much more difficult.
I will scroll down here, okay, so, CountRedRows.
Well, now we have to do all this pre-work to figure out what the FirstRow is, so MyRange.Rows(1)-- so whatever the first row is in the range, give me the row number.
And then RowCount = MyRange.Rows.Count, you know, so rows is a collection of all the rows in my range and this will tell me how many there are.
I had to do that just so that way I could do MyRange.Rows-- give me the last row and tell me what row it is.
Same thing for the columns here.
Then once I know that I have two loops, For i = FirstRow To LastRow and then j = FirstCol To LastCol-- so, as you can imagine, we're going through Row 2 or 3, Row 4, Row 5-- within each of those rows, we're going through Column 2, 3, 4, 5, and 6.
Check and see if the row-- or if the cell-- at the intersection of this row and this column is red, if it is, then yeah, we increment CountRedRows by 1.
But we also have to skip the rest of the columns in this row, so this is where "Exit For" comes in.
Exit For says, Hey, I know we have a For/Next loop here-- the For j = 2 to 6, I think, in this case-- but if we get to this Exit For inside the If statement, then just skip the rest of the columns in this loop.
Alright, so there's a good way to Exit the current loop: if I needed to Exit the i loop here, I'd have to set something-- some sort of a variable in weight-- til I got outside of the next j.
So Exit For is handy if you're trying to cancel the current loop.
It won't cancel the i loop, we're still just going to move on to the next row.
So, let's go and take a look.
So =CountRedRows of this whole range, and hopefully bring it less than 9 because there's a couple of [ Inaudible at 00:02:55 ].
So, that row has two red cells, this row has two red cells, and you get just the number of rows that contain red.
It sounds like a simple little change, doesn't it?
But it adds a lot of complexity to the macro, from that macro down to this one.
Alright, well, hey I want to thank you for stopping by.
Again, hey, for Macros, check out Tracy and my book, "VBA And Macros".
There's four editions: Excel 2003, 2007, 2010, or 2013.
Or if you like to watch videos, there's a live lessons Power Macros-- or Excel Macros-- in VBA.
I want to thank you for stopping by, we'll see you next time for another netcast from MrExcel.
 

Forum statistics

Threads
1,223,699
Messages
6,173,905
Members
452,536
Latest member
Chiz511

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top