loop thru cells in current row

davidmyers

Board Regular
Joined
Jan 29, 2017
Messages
88
Office Version
  1. 2016
Platform
  1. Windows
Hi, Need a little guidance (or was that gui dance?)

I'm trying to check the font color of each of each cell in the current row, not sure how to do it, this is what I have so far - I'm getting an error of Type mismatch on the range

Code:
Sub KeepRed(defaultcolor)
Dim rng As Range, cell As Range
Set rng = ActiveCell.Row
   For Each cell In rng
       If Not (cell.Font.ColorIndex = 3) Then
           cell.Font.ColorIndex = defaultcolor
       End If
   Next cell
End Sub

Don't know if I need a range to loop thru current row?

Any help appreciated
David
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,
try changing this line

Code:
Set rng = ActiveCell.Row

for this

Code:
Set rng = Rows(ActiveCell.Row)

and see if this resolves the error

Dave
 
Upvote 0
Try
Code:
Sub KeepRed(defaultcolor)
Dim rng As Range, cell As Range
Set rng = Intersect(activecell.EntireRow, ActiveSheet.UsedRange)
   For Each cell In rng
       If Not (cell.Font.ColorIndex = 3) Then
           cell.Font.ColorIndex = defaultcolor
       End If
   Next cell
End Sub
 
Upvote 0
Thanks for your quick responses, I used Fluff's solution 'caused it solved another issue I was having which was it took time to execute - blue circle for a few seconds whenever I activated the code - with Fluffs code it's instantaneous.

Thanks all, much appriciated
David
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

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