Sub Routine not working inside Sheet

kwesmc

Board Regular
Joined
Jan 25, 2008
Messages
83
I have as simple module which changes the font based on the name I select inside my worksheet. The code works well when I'm in the VBE and press F5, but when I'm in the worksheet and select a choice in the cell like "YES", the font reverts back to the default font. I know I'm doing something stupid with my code, but can't figure out what. Can someone help please? See code below.
Thanks so much!
Ken Mc

Code:
Sub test()
Dim cell As Object

For Each cell In Range("B2:M35")
If UCase(cell.Value) Like "*YES*" Or UCase(cell.Value) Like "*MODERATE*" Then
cell.Font.Name = "Comic Sans MS"
cell.Font.Bold = True
End If

Next cell

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Ken

How are you executing the code from the sheet?
 
Upvote 0
I can create a button, but what I was wondering if there was anyway to make the code change the font after input into a cell?
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

   If Target.CountLarge > 1 Then Exit Sub
   If Intersect(Target, Range("B2:M35")) Is Nothing Then Exit Sub
Application.EnableEvents = False
   If UCase(Target.Value) Like "*YES*" Or UCase(Target.Value) Like "*MODERATE*" Then
      Target.Font.Name = "Comic Sans MS"
      Target.Font.bold = True
   End If
Application.EnableEvents = True

End Sub
This needs to go in the relevant sheet module
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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