For Loop to Change Cell Colors and Ignore Cells Based on Color?

shaun27

New Member
Joined
Jul 22, 2014
Messages
5
Hi everyone, hoping for your expertise. I am working with a set of data and trying to create a macro that, based on the cells that are selected, will return the highest value with the first button click, the second highest value with the second button click, etc.

I have put together the below, which is providing the highest number of the selected cells and inputting it in the correct cell. However, in order to minimize the amount of code I need to write, I want to create a For loop that, after finding the cell range of the first highest number, will copy the value to B40 and then change the interior of the cell range from the selected cells that was used red. That way, when I click on the button again, the macro will ignore the red cell and find the next largest value in the selection and record that in B41, and so on and so forth.

Would really appreciate your help!!!

Sub Decider()

Set rng = Selection
newCell = 0

For Each cell In rng
If cell.Value > newHighest And cell.Interior.Color <> RGB(255, 0, 0) Then
newHighest = cell.Value
End If
Next
Range("B40") = newHighest

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This....this is tricky. But not impossible. So long as you promise to leave the B40:Bxxx range alone...let's do this.


Code:
Sub Decider()    
    Dim Rng As Range
    Dim output As Range
    Dim nhRow As Long
    Dim nhCol As Long
    Dim newHighest As Long
    
    
    Set output = Range(Cells(40, 2), Cells(100, 2))
    Set Rng = Selection
    newHighest = 0
    
    For Each cell In Rng
        If cell.Value > newHighest And cell.Interior.Color <> RGB(255, 0, 0) Then
        
            newHighest = cell.Value
            nhRow = cell.Row
            nhCol = cell.Column
            
        End If
    Next


    Cells(nhRow, nhCol).Interior.Color = RGB(255, 0, 0)
    
    For Each cell In output
        If cell.Value = "" Then
            cell.Value = newHighest
            Exit For
        End If
    Next
End Sub
 
Upvote 0
Thanks, Neon! I'm trying to run it but am experiencing a run error related to the portion of the code in which we're trying to change the interior color of the selected cell: Method '_Default' of object 'Range' failed.


To be honest, I'm really stumped as to why that's being flagged as a bug. Did it work on your end?
 
Upvote 0
Try replacing:
Code:
[COLOR=#333333]Interior.Color = RGB(255, 0, 0)[/COLOR]
with
Code:
[COLOR=#333333][FONT=Trebuchet MS]Interior.ColorIndex = 3[/FONT][/COLOR]

I seem to remember macs and pcs handling color codes differently. Other than that, not sure.
 
Upvote 0
Interesting. So the color portion works now and correctly highlights the highest number cell. However, when I click on the button again, it does not change the color of the cell containing the second-highest figure, nor record the new value. Is there an issue using the double quotation marks (hopefully not another mac-specific problem)?
 
Upvote 0
Interesting. So the color portion works now and correctly highlights the highest number cell. However, when I click on the button again, it does not change the color of the cell containing the second-highest figure, nor record the new value. Is there an issue using the double quotation marks (hopefully not another mac-specific problem)?

Code:
Sub Decider()    Dim Rng As Range
    Dim output As Range
    Dim nhRow As Long
    Dim nhCol As Long
    Dim newHighest As Long
    
    
    Set output = Range(Cells(40, 2), Cells(100, 2))
    Set Rng = Selection
    newHighest = 0
    
    For Each cell In Rng
        If cell.Value > newHighest And cell.Interior.ColorIndex <> 3 Then
        
            newHighest = cell.Value
            nhRow = cell.Row
            nhCol = cell.Column
            
        End If
    Next




    Cells(nhRow, nhCol).Interior.ColorIndex = 3
    
    For Each cell In output
        If cell.Value = "" Then
            cell.Value = newHighest
            Exit For
        End If
    Next
End Sub

The issue is I forgot to tell you to change the other portion of the code that references the color. Try this code.
 
Upvote 0
Ah, silly me, works like a charm. You're awesome, thank you so, so much for the help!!! Really appreciated!
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

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