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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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