Apply macro to arbitrary highlighted selected cells

Excel_newbie2020

New Member
Joined
Dec 26, 2019
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
As a total newbie to Macros, I thought I could attempt something brave. Much of it worked, but when I tried to apply it to an arbitrary selection of highlighted cells (rows x columns), it failed. Hence the plead for from you pundits out there.
The requirements are to replace all numbers between the range of 1 and 9 in any set of highlighted cells where the font color is RED only to 1. The process is to first select a highlighted range of cells and then run the Macro for that range, such that only the RED numbers magically change to 1. This is what I tried, but if does not work on the highlighted cells. Wonder what's missing?
Thanks for your assistance in helping me out.

Sub Red()
Dim r As Range
Set r = Selection
For Each r In Selection
v = r.Value
If v <> "" And IsNumeric(v) Then
If v >= 1 And v <= 9 And Selection.Font.Color = vbRed Then
r.ClearContents
r.Value = "1"
End If
End If
Next r

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel
Change "Selection" to "r"
you may also want to remove the quotes from this line r.Value = "1" otherwise it will be treated as text, not a number
 
Upvote 0
Absolutely Brilliant solution !!!!
Thank You, Thank You, Thank You !!!!!
Deeply appreciated... and a Happy New Year!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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