Dynamic Range LastRow (Excluding Last Row)

lamarh755

New Member
Joined
Jan 28, 2020
Messages
35
Office Version
  1. 2013
I have a macro that I am trying to write that counts the total number of yellow, red and green cells in Column V (V8 to V Last Row Minus 1 Row) of Sheet "Day Shift". I am trying to have the total count of colors appear on Sheet "Summary" Range D20 for Green, F20 for Yellow and H20 for Red. I have a code but the code includes the last row, which I am trying to exclude. Any assistance with this code would be greatly appreciated. I am new to this. Here is what I have

Sheets("Day Shift").Activate

Dim rng As Range, rCell As Range, Total As Double, LastRow As Long

LastRow = Cells(Cells.Rows.Count, "V").End(xlUp).Row
Set rng = Range("V8:V" & LastRow)


For Each rCell In rng
If rCell.DisplayFormat.Interior.Color = RGB(255, 255, 0) Then _
Total = Total + 1
Next rCell

Sheets("Summary").Activate

Sheets("Summary").Range("F20") = Total
 

Attachments

  • VBA Capture IV.PNG
    VBA Capture IV.PNG
    155.7 KB · Views: 25

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try using

VBA Code:
Set rng = Range("V8:V" & LastRow-1)
 
Upvote 0
I must be doing something wrong. I entered the range as recommended and it is still giving me a total of 9 yellow cells when it should be giving me 8.
 
Upvote 0
set your total variable at the beginning to zero
Rich (BB code):
Sheets("Day Shift").Activate
Dim rng As Range, rCell As Range, Total As Double, LastRow As Long
Total = 0
LastRow = Cells(Cells.Rows.Count, "V").End(xlUp).Row
Set rng = Range("V8:V" & LastRow)
For Each rCell In rng
If rCell.DisplayFormat.Interior.Color = RGB(255, 255, 0) Then _
Total = Total + 1
Next rCell
Sheets("Summary").Activate
Sheets("Summary").Range("F20") = Total
 
Upvote 0
The grand total cell in the last row happens to be yellow. When I plug another color (red or green) into the code I get the correct count. Seems to still be an issue with it counting the last row in the range.
 
Upvote 0
Try this instead. BTW, when posting code, please use code tags - see my signature block below for more on that.

VBA Code:
LastRow = Columns("V").Find(What:="*", SearchDirection:=xlPrevious).Row
Set rng = Range("V8:V" & LastRow - 1)
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,980
Members
449,201
Latest member
Lunzwe73

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