Vb Code Doesn't Work

PIJim

Board Regular
Joined
Jul 29, 2005
Messages
143
Can anyone tell me why this code doesn't work?
I used this same code w/ numbers instead of letters and it worked. = Cells fill with appropriate color base on value entered.

Thanks,
Jim

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     
    Dim Cell As Range
    Dim Rng1 As Range
     
    On Error Resume Next
    Set Rng1 = ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas, 1)
    On Error GoTo 0
    If Rng1 Is Nothing Then
        Set Rng1 = Range(Target.Address)
    Else
        Set Rng1 = Union(Range(Target.Address), Rng1)
    End If
    For Each Cell In Rng1
        Select Case Cell.Value
        Case vbNullString
            Cell.Interior.ColorIndex = xlNone
            Cell.Font.Bold = False
        Case 0
            Cell.Interior.ColorIndex = 9
            Cell.Font.Bold = True
        Case MA
            Cell.Interior.ColorIndex = 38
            Cell.Font.Bold = True
        Case IN
            Cell.Interior.ColorIndex = 15
            Cell.Font.Bold = True
        Case AD
            Cell.Interior.ColorIndex = 39
            Cell.Font.Bold = True
        Case SC
            Cell.Interior.ColorIndex = 37
            Cell.Font.Bold = True
        Case OC
            Cell.Interior.ColorIndex = 35
            Cell.Font.Bold = True
        Case PM
            Cell.Interior.ColorIndex = 27
            Cell.Font.Bold = True
        Case Else
            Cell.Interior.ColorIndex = xlNone
            Cell.Font.Bold = False
        End Select
    Next
     
End Sub
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
For a start I would suggest you lose the On Error Resume Next part of the code.

It could just be hiding errors.

Also I would suggest you enclose the text in quotes.
Code:
Case "MA"
Otherwise VBA will be regarding MA etc as variables.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,968
Messages
5,856,576
Members
431,820
Latest member
martel_9

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