VBA code to check if any one cell is colored in a range.

mchilapur

Board Regular
Joined
Apr 14, 2014
Messages
126
Dear all
I have put up the below code under 'ThisWorkbook' section and my intent is to check if a rectangle shape (its 13th) text is "YES" and if none of the cell in range "E36:E45" is colored (GREEN), then it must not allow user to move forward to next sheet..If atleast any one cell is colored, then it must not do anything(Must allow).

I dont know what mistake i am doing here...
Below code throws a message even if atleast one cell is colored..:( (While it must do this if none of the cells are colored)

Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)Dim Counter As Long
Dim Gcell As Range
Dim flg As Boolean
Counter = 0
If Sh.Shapes("Rectangle 13").TextFrame.Characters.Text = "YES" Then
For Each Gcell In Range("E36:E45")
        If Gcell.Interior.Color = 65280 Then Counter = Counter + 1
    Next
    If Counter = 0 Then
        Application.EnableEvents = False
        Sh.Activate
        Application.EnableEvents = True
        MsgBox "None of the cells are colored " & Sh.Name, vbExclamation, "Check of color cells"
    End If
  End If
End Sub

Please suggest.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)Dim Counter As Long
Dim Gcell As Range
Dim flg As Boolean
Counter = 0
If Sh.Shapes("Rectangle 13").TextFrame.Characters.Text = "YES" Then
For Each Gcell In Range("E36:E45")
        If Gcell.Interior.Color = 65280 Then Counter = Counter + 1 [COLOR=#ff0000][B]: Exit Sub[/B][/COLOR]
    Next
    If Counter = 0 Then
        Application.EnableEvents = False
        Sh.Activate
        Application.EnableEvents = True
        MsgBox "None of the cells are colored " & Sh.Name, vbExclamation, "Check of color cells"
    End If
  End If
End Sub
 
Upvote 0
Or maybe this:
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)Dim Counter As Long
    Dim Gcell As Range
    Dim flg As Boolean
    
    If Sh.Shapes("Rectangle 13").TextFrame.Characters.Text = "YES" Then
        For Each Gcell In Range("E36:E45")
            If Gcell.Interior.Color = 65280 Then Exit Sub
        Next
    End If
    Application.EnableEvents = False
    Sh.Activate
    Application.EnableEvents = True
    MsgBox "None of the cells are colored " & Sh.Name, vbExclamation, "Check of color cells"
End Sub
 
Upvote 0
How is the cell getting its color? From the user filling the cell or from Conditional Formatting?
 
Upvote 0
cells are colored Using below code

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const WS_RANGE As String = "E36:E45"


On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
  With Target
     If Selection.Interior.Color = 16777215 Then
     Selection.Interior.Color = 65280
Else
     Selection.Interior.Color = 16777215
     End If
  End With
End If


ws_exit:
Application.EnableEvents = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,359
Members
451,642
Latest member
mirofa

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