VBA Format Cell Font based on Formula contents

OVH

New Member
Joined
Nov 27, 2012
Messages
25
I'm new to VBA and I'm trying to select a range and format all of the cells in the range differently based on the cell contents. Basically if the cell is a link to another workbook then change font to red, if the cell is a link to another sheet (same workbook) then format as green, if a formula in the same sheet then font should be blue, if it's a manual input format as black.

If cell.formula contains a "]" and a "!", then
cell.font.color=255
else
If Cell.Formula contains a "!" but not a "]", then
cell.font.color=34310
else
If Cell.Formula contains a "=" but not a "]" or a "!", then
cell.font.color=16711680
Else
Cell.font.color=0
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

rconverse

Well-known Member
Joined
Nov 29, 2007
Messages
1,187
I think you could accomplish this with just conditional formatting. Looking at your first if scenario, this formula would work:

=ISNUMBER(SEARCH("]", A1)+SEARCH("!", A1))

You could just apply the same type logic in your other formulas and then apply those rules to your range.

HTH,
Roger
 

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,183
Office Version
  1. 2013
Platform
  1. Windows
OVH,

VBA-wise..... Perhaps try this to format the selected range based on your stated criteria.

Code:
Sub FMat()
For Each Cell In Selection
If Cell.Formula Like "*]*" And Cell.Formula Like "*!*" Then
Cell.Font.Color = 255
ElseIf Not Cell.Formula Like "*]*" And Cell.Formula Like "*!*" Then
Cell.Font.Color = 34310
ElseIf Not Cell.Formula Like "*]*" And Not Cell.Formula Like "*!*" And Cell.Formula Like "=*" Then
Cell.Font.Color = 16711680
Else
Cell.Font.Color = 0
End If
Next Cell
End Sub

Hope that helps.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,340
Messages
5,641,576
Members
417,223
Latest member
jelena_

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
Top