# Macro to change cell color based on another cell color



## mayoung (Dec 20, 2022)

I have two pieces of code I have playing around with all afternoon and need some help.  What I am trying to do is loop through column-C (W/O #) (see below image) and change the cell color to what's in column-B (Branch), Only looking at the visible cells after applying a filter on column-C to find all the rows with blue cells. I have looked at different loops searching the internet and other code loops I have, but can not figure this one out. Please help!! Thank You In Advance.


```
If ActiveCell.Interior.Color <> ActiveCell.Offset(0, -1).Interior.Color Then
ActiveCell.Interior.Color = ActiveCell.Offset(0, -1).Interior.Color
End If
```


```
Me.Range("C1").Interior.Color = Me.Range("B1").Interior.Color
```

BranchW/O #04/HILW5558704/HILW5599404/HILW5668804/HILW5713304/HILW5727804/HILW5742404/HILW5747204/HILW5748704/HILW57629


----------



## Micron (Dec 20, 2022)

You don't say much about what happens or show a lot of code? This works for me insofar as color, but it seems that the empty rows are removed. Can't see why yet though.

```
Sub SetInteriorColor()
Dim cl As Range, rng As Range
Dim Lrow As Long
    
 Lrow = Cells(Rows.count, "C").End(xlUp).Row
 Set rng = ActiveSheet.Range("C2:C" & Lrow)
    
 For Each cl In rng.SpecialCells(xlCellTypeVisible)
      cl.Offset(0, -1).Interior.Color = cl.Interior.Color
 Next cl

End Sub
```


----------



## mayoung (Dec 20, 2022)

Micron said:


> You don't say much about what happens or show a lot of code? This works for me insofar as color, but it seems that the empty rows are removed. Can't see why yet though.
> 
> ```
> Sub SetInteriorColor()
> ...



Micron, The code did the reverse I just had to flip it and now it works perfect. I would have never figured this out. Thank You

```
cl.Interior.Color = cl.Offset(0, -1).Interior.Color
```


----------



## Micron (Dec 20, 2022)

ooops! Somewhere between reading and coding I mixed up the requirement.
Glad to see it works for you.


----------

