screen flickers due to worksheet selection change event.

jryan15

Board Regular
Joined
Jan 27, 2005
Messages
168
i wrote a worksheet selection change event script (below) to illuminate target cell location by changing the cell interior and font color on the row and column headers. the code works but i get a lot of screen flickering as it executes even with the screen updating turned off. i don't get any flickering when i turn this function off. i'd appreciate any comments or recommendations. thanks!

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim tmp As Range, A As Range, B As Range, C As Range
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error GoTo z
        
    Set A = Range("A6")
    Set B = A.Offset(0, 1).End(xlToRight)
    Set C = A.Offset(1, 0).End(xlDown)
   
    'Reset font colors and interiors
    With Range(A.Offset(0, 1), B)
        .Interior.ColorIndex = 19
        .Font.ColorIndex = 5
        .Font.Italic = False
        .Font.Bold = False
    End With
    With Range(A.Offset(1, 0), C)
        .Interior.ColorIndex = 19
        .Font.ColorIndex = 5
        .Font.Italic = False
        .Font.Bold = False
    End With
    
    'exit if in wrong area
    If Target.Cells.Count > 1 Then GoTo z
    If Target.Row <= A.Row Or Target.Column <= A.Column Then GoTo z

    With Cells(A.Row, Target.Column)
        .Interior.ColorIndex = 4
        .Font.ColorIndex = 3
'        .Font.Italic = True
        .Font.Bold = True
    End With
    With Cells(Target.Row, A.Column)
        .Interior.ColorIndex = 4
        .Font.ColorIndex = 3
'        .Font.Italic = True
        .Font.Bold = True
    End With

z:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
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.

Forum statistics

Threads
1,215,817
Messages
6,127,041
Members
449,356
Latest member
tstapleton67

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