Finding last Green (5287936) Filled Cell in Row

endrese

New Member
Joined
Mar 25, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Happy Friday, I am attempting to find the last green (color Id=5287936) filled cell for each row in a data set. Once found I need to report the appropriate column label - (text present in row 4), for each identified green filled cell. I know I would need a for loop in order to loop through the whole dataset (multiple rows), but thought I'd start with just one row. My code is as follows:
_____________________________________________________________________________________________________
Sub findgreen()

ActiveWorkbook.RefreshAll 'Refreshes/Pulls data

Dim s As Worksheet
Set s = ActiveSheet ' look on the active sheet
'Set s = ThisWorkbook.Worksheets("Sheet1") ' look on a specific sheet

Dim color As Long
color = 5287936 ' enter the result of the statement above here

Dim row As Integer
row = 1

Dim col As Integer

For col = s.Columns.Count To 1 Step -1
If s.Cells(row, col).Interior.color = color Then Exit For
Next

s.Activate
s.Cells(row, col).Select ' *** Debug stopped here


End Sub

___________________________________________________________________________________________________________________


However, I am receiving an error: application-defined or object-defined error at the " s.Cells(row,col).Select" line

Again, I am new to this. Any input in terms of code adjustment or resources would be greatly appreciated.

Thank you in advance,
 
I've been reading that there is no way to write a macro determining the last cell in a row with specified color (if conditionally formatted)
Whatever you read is wrong. However, it cannot be done using the Find method. We have to return to your original approach, modifying one line of code as shown:

Rich (BB code):
If s.Cells(row, col).DisplayFormat.Interior.color = color Then Exit For

Also re-read post #9 if you are going to this, which explains why you were getting the error.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Thank you for your patience.
I tested the initial code (#1) on an blank workbook with a random cell filled green on the first row and it selected the green cell no problem.
However, with or without the modification listed above (shown below) I am still receiving the application defined error when I input the same code on the desired workbook.
I really appreciate the help I've been receiving and I don't understand how the book I was reading was wrong.
Is there another reference/resource I could utilize to help troubleshoot this problem?


Code is as follows:

Sub mrexcl()

ActiveWorkbook.RefreshAll 'Refreshes/Pulls data

Dim s As Worksheet
Set s = ActiveSheet ' look on the active sheet
'Set s = ThisWorkbook.Worksheets("Sheet1") ' look on a specific sheet

Dim color As Long
color = 5287936 ' enter the result of the statement above here

Dim row As Integer
row = 1

Dim col As Integer

For col = s.Columns.Count To 1 Step -1
If s.Cells(row, col).DisplayFormat.Interior.color = color Then Exit For
Next

s.Activate
s.Cells(row, col).Select ' *** Debug stopped here





End Sub





Thank you in Advance,

Eileen Endres
 
Upvote 0
Your code works perfectly for me when there is a green cell. When there is not a green cell, I get an error on the same line you pointed out. Therefore I assume that you do not have a green cell. Make sure your green cell has a color number of exactly 5287936. The RGB number is (0, 176, 80).
 
Upvote 0
I can confirm that the color in the current green cells is the standard green, 5287936. The conditional formatting rule that changes cells to this green color, following the refreshing of data is seen in the image.
Could the application defined error be a result of the conditional formatting rule?
 

Attachments

  • C-rule.PNG
    C-rule.PNG
    47 KB · Views: 4
Upvote 0
I don't know why it doesn't work in your file. All I can say is that it works for me, using conditional formatting. Unfortunately the picture doesn't help. I have attached my test file so you can compare it to yours.

In my file the cell turns green if the value is 12. When you click the button, it will find the last green cell on the row that has the currently selected cell. (Note that your code always looks in row 1.)

 
Upvote 0
When I run the code I receive error 400. Any suggestions?
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,666
Members
448,977
Latest member
moonlight6

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