On error run next case statement

excelis

New Member
Joined
Jun 3, 2010
Messages
38
Dear All,

in a previous thread which you can see here,

http://www.mrexcel.com/forum/showthread.php?p=2617843

i was advised to use the Case statement which worked perfectly. In some cases however, where my active cell is for example in position C3 the case ActiveCel.Offset(1,-4) gives of course an error!

Do you know how and where i should write that if there is an error, ignore it and continue with the next case statement??

Thank you in advance!!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Maybe it would be easier to post once again the code also here


Code:
 Select Case True
            
                    Case ActiveCell.Offset(1, 0) = 0
                      ActiveCell.Offset(1, 0).Range("A1").Select
                      With Selection.Interior
                       .Pattern = xlSolid
                       .PatternColorIndex = xlAutomatic
                       .Color = 65535
                       .TintAndShade = 0
                       .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, 1) = 0
                      ActiveCell.Offset(1, 1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, -1) = 0
                      ActiveCell.Offset(1, -1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, 2) = 0
                      ActiveCell.Offset(1, 1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, -2) = 0
                      ActiveCell.Offset(1, -1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, 3) = 0
                      ActiveCell.Offset(1, 1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, -3) = 0
                      ActiveCell.Offset(1, -1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, 4) = 0
                      ActiveCell.Offset(1, 1).Range("A1").Select
                      With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
            
                    Case ActiveCell.Offset(1, -4) = 0
                      ActiveCell.Offset(1, -1).Range("A1").Select
                       With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 65535
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                      End With
End Select
 
Last edited:
Upvote 0
Perhaps ...
Code:
Sub x()
    Dim j           As Long
    Dim k           As Long
 
    With ActiveCell.Offset(1)
        If .Value = 0 Then
            j = 0

        Else
            For j = 1 To 4
                If j + .Column <= Columns.Count Then
                    If .Offset(, j) = 0 Then
                        k = j
                        Exit For

                    ElseIf .Column - j >= 1 Then
                        If .Offset(, -j) = 0 Then
                            k = -j
                            Exit For
                        End If
                    End If
                End If
            Next j
            If j > 4 Then Exit Sub
        End If
 
        With .Offset(, k).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End With
End Sub
 
Upvote 0
Thank you shg for the reply! I have tried the code but it gives me an error on

With . Offset(, k).Interior

saying error 438 object doesn't support this property or method

Any ideas why is this happening?
 
Upvote 0
You have a space between the dot and the Offset.
 
Upvote 0
Thank you! you were correct! I put a space by accident! I also modified the k=j to k=1 or -1 as i wanted to move one space and i added the active cell move to the position it paints. It works now like a charm!!
Code:
    Dim j As Long
    Dim k As Long
 
    With ActiveCell.Offset(1)
        If .Value = 0 Then
            j = 0

        Else
            For j = 1 To 4
                If j + .Column <= Columns.Count Then
                    If .Offset(, j) = 0 Then
                        k = 1
                        Exit For

                    ElseIf .Column - j >= 1 Then
                        If .Offset(, -j) = 0 Then
                            k = -1
                            Exit For
                        End If
                    End If
                End If
            Next j
            If j > 4 Then Exit Sub
        End If
        
 
        With .Offset(, k).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        ActiveCell.Offset(1, k).Range("A1").Select
    End With
End Sub
Thank you very much!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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