Modification of ThisWorksheet Macro

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
I have the following Macro that is working fine, EXCEPT if I select a cell that has PREVIOUSLY been color-formatted manually - the
below Macro NULLIFIES - meaning it resets my previously colored cell's format is set to "ColorIndex" of None. I need for the below
Macro (to be modified) so as to test if the currently selected cell "ALREADY HAS A COLORINDEX APPLIED", and if so, RESTORE this
Formatting AFTER MOVING AWAY from the cell. Can anyone help me with this modification, please...

Thanks in Advance..
Jim

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Static OldCell As Range
If bFlag = True Then
If Application.CutCopyMode = 0 Then
If Not OldCell Is Nothing Then
OldCell.Interior.ColorIndex = xlColorIndexNone
'OldCell.Borders.LineStyle = xlLineStyleNone
End If
Set OldCell = Target
OldCell.Interior.ColorIndex = 6
'OldCell.Borders.LineStyle = xlContinuous
Else
If OldCell Is Nothing Then
Set OldCell = Target
Else
Set OldCell = Union(OldCell, Target)
End If
End If
End If
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Not really understand what the whole thing started, but it looks like you trigger the macro on sheet selection change and not cell change in worksheet.

Since I don't know where bFlag condition comes from, I have to remark that out to test the code ?

VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

Static OldColor As Long
Static OldCell As Range

On Error Resume Next
'If bFlag = True Then
    If Application.CutCopyMode = 0 Then
        If Not OldColor < 0 Then
            OldCell.Interior.ColorIndex = OldColor
        Else
            OldCell.Interior.ColorIndex = xlColorIndexNone
        End If
        Set OldCell = Target
        OldColor = Target.Interior.ColorIndex
        Target.Interior.ColorIndex = 6
    Else
        If Target Is Nothing Then
            Set OldCell = Target
        Else
            Set OldCell = Union(OldCell, Target)
        End If
    End If
'End If
On Error GoTo 0

End Sub

The reason I added On Error Resume Next was just because during initial run, the OldCell was not yet set (not defined) and will produce an error on line OldCell.Interior.ColorIndex = OldColor line. Otherwise, not problem
 
Upvote 0
Solution
Thanks Zot

In my Workbook_Open Macro I have a line:
bFlag = True

Thanks to you I got it Working exactly like I wanted it...

Jim
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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