Unable to get the error fixed in VBA

asad

Well-known Member
Joined
Sep 9, 2008
Messages
1,434
Hello Guys,

I was able to put two codes together and it worked for me, but when I tried to insert another line, it gave me an error. I am unable to fix that error. Can you please help me? The error message is :
Error in Worksheet Change Event Code: Application-defined or Object-defined error
The actual code is:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
Dim Counter As Long
For Counter = 1 To Selection.Rows.Count
If Counter Mod 2 = 1 Then
Selection.Rows(Counter).Interior.ColorIndex = 15
End If
Next Counter
On Error GoTo Handler
Application.EnableEvents = False
    
    Set rng = Intersect(Target, Range("I4:BC200"))
    
    If rng Is Nothing Then
        GoTo My_Exit
    Else
        For Each c In rng
            Select Case c.Value
                Case "RDO"
                    c.Interior.ColorIndex = 19
                    c.Font.ColorIndex = 41
                    c.Font.Bold = True
                Case "OFF"
                    c.Interior.ColorIndex = 35
                Case "1", "2", "4", "128", "139", "142", "143", "145", "749"
                    c.Interior.ColorIndex = 45
                [COLOR=red][B]Case c.Value <> c.Offset(, -8).Value
                    c.Font.ColorIndex = 3
[/B][/COLOR]                
            End Select
        Next c
        
    End If
    
My_Exit:
Application.EnableEvents = True
Exit Sub
Handler:
MsgBox "Error in Worksheet Change Event Code: " & Err.Description
Resume My_Exit
End Sub
The line in red is the one that triggered that error message. How can I fix that?

Any help is appreciated.

Asad
 
Code:
    For Each c In Rng
        Select Case c.Value
            Case "RDO"
                c.Interior.ColorIndex = 19
                c.Font.ColorIndex = 41
                c.Font.Bold = True
            Case "OFF"
                c.Interior.ColorIndex = 35
            Case "1", "2", "4", "128", "139", "142", "143", "145", "749"
                c.Interior.ColorIndex = 45
        End Select
        
        If c.Value <> c.Offset(, -8).Value Then
            c.Font.ColorIndex = 3
        End If
    Next c
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It works, but then there is another problem with this code. I am not able to find what is wrong. First I will post a screen shot.
Excel Workbook
ABCDEFGHI
1
2
3
4Monday
539224
6RDO
7
8OFFOFF
9206206
10201204
11202202
12203203
13204204
14205205
151414
16RDORDO
173737
18OFFOFF
1933
2023
2118
2251
Sheet2


You see 204 in I10, it is in red and that's the way it should be. But it makes the cell I11 go grey. It should stay white as was before. I didn't even change the value in I11. Why did it change then? Can't understand :confused:

Asad
 
Upvote 0
Finally got it working the way I wanted it to.:)
It is like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, c As Range
Dim Counter As Long
For Counter = 1 To Selection.Rows.Count
        If Counter Mod 2 = 1 Then
            Selection.Rows(Counter).Interior.ColorIndex = 15
        End If
    Next Counter
On Error GoTo Handler
Application.EnableEvents = False
 
    Set rng = Intersect(Target, Range("I4:BC200"))
 
    If rng Is Nothing Then
        GoTo My_Exit
    Else
         For Each c In rng
        Select Case c.Value
            Case "RDO"
                c.Interior.ColorIndex = 19
                c.Font.ColorIndex = 41
                c.Font.Bold = True
            Case "OFF"
                c.Interior.ColorIndex = 35
            Case "1", "2", "4", "128", "139", "142", "143", "145", "749"
                c.Interior.ColorIndex = 45
        End Select
 
        If c.Value <> c.Offset(, -8).Value Then
            c.Font.ColorIndex = 3
            Else
                If c.Value = "RDO" Then
                c.Interior.ColorIndex = 19
                c.Font.ColorIndex = 41
                c.Font.Bold = True
                Else
                    If c.Value = "OFF" Then
                    c.Interior.ColorIndex = 35
                    c.Font.ColorIndex = 1
                    Else
                        If c.Value = c.Offset(, -8).Value Then
                        c.Font.ColorIndex = 1
                        c.Font.Bold = False
                        End If
                    End If
                End If
        End If
 
    Next c
 
    End If
 
 
My_Exit:
Application.EnableEvents = True
Exit Sub
Handler:
MsgBox "Error in Worksheet Change Event Code: " & Err.Description
Resume My_Exit
End Sub

Thanks a lot shg. Without your help, I wouldn't have been able to do this.

Asad
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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