VBA Check for interior cell color before close

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with a
VBA Code:
Private Sub Workbook_Close()
code, where I would like to provide the code to check for an interior cell color red in the range of B11:AI180.

I was experimenting with the following but I cannot make it work.

VBA Code:
Dim response As VbMsgBoxResult
response = MsgBox("Highlighted cells are found on your sheet.  Do you want them to be corrected", vbYesNo)

   If response = vbYes Then
    MsgBox "Please make the corrections."
  Exit Sub

   If response = vbNo Then
     'MsgBox "Continues to close document."
   Exit Sub

End If

Do you think you can help?

Many thanks!
-Pinaceous
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How were the cells colored? "Conditional Format" or "Format Cells"?
 
Upvote 0
Hi jolivanes,

They are colored through VBA criteria.

Mostly cells that are supposed to be filled in by the user but may have been left blank, where it triggers a corresponding cell to interior color red.

So, with multiple users using the same document, the red cells can be overlooked.

That is why I'm trying accomplish this code now.

Thanks!
pinaceous
 
Upvote 0
Put this in the "ThisWorkbook" module.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim c As Range, rngCol As Range
    For Each c In Range("B11:AI180")
        If c.Interior.Pattern <> xlNone Then
            If rngCol Is Nothing Then
                Set rngCol = c
                    Else
                Set rngCol = Union(rngCol, c)
            End If
        End If
    Next c
    If rngCol Is Nothing Then
        MsgBox "No colored cells in range!"
            Else
        MsgBox "Found " & rngCol.Count & " colored cells in:" & vbLf & rngCol.Address
    End If
End Sub
 
Upvote 0
Hey thanks jolivanes!

I'll try it out now! Thanks so much!
 
Upvote 0
Hey jolivanes!

It works really great! But do you know if you can add, if the code finds a interior red colored cell that it can possibly stop the closing of the workbook?

Is this possible?

Thanks,
pinaceous
 
Upvote 0
Do you want to keep it open or just get rid of the colors?
 
Upvote 0
Maybe you want a choice of "uncoloring" or not.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim c As Range, rngCol As Range, msg1 As String
    For Each c In Range("B11:AI180")
        If c.Interior.Pattern <> xlNone Then
            If rngCol Is Nothing Then
                Set rngCol = c
                    Else
                Set rngCol = Union(rngCol, c)
            End If
        End If
    Next c
    If rngCol Is Nothing Then
        MsgBox "No colored cells in range!"
            Else
        MsgBox "Found " & rngCol.Count & " colored cells in:" & vbLf & rngCol.Address(0, 0)
    End If
    msg1 = MsgBox("Undo the colors in the colored cells?", vbYesNo, "Keep Colored Cells Or Not.")
        If msg1 = vbYes Then
            rngCol.Interior.Pattern = xlNone
                Else
            MsgBox "We'll continue closing this Workbook."
        End If
End Sub
 
Last edited:
Upvote 0
Hey jolivanes,

That is great!

Do you know in lieu of
VBA Code:
rngCol.Interior.Pattern = xlNone
that the workbook can be prevented from closing alltogether?

Is this possible?

Thanks!
pinaceous
 
Upvote 0
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim c As Range, rngCol As Range, msg1 As String
    For Each c In Range("B11:AI180")
        If c.Interior.Pattern <> xlNone Then
            If rngCol Is Nothing Then
                Set rngCol = c
                    Else
                Set rngCol = Union(rngCol, c)
            End If
        End If
    Next c
    If rngCol Is Nothing Then
        MsgBox "No colored cells in range!"
            Else
        MsgBox "Found " & rngCol.Count & " colored cells in:" & vbLf & rngCol.Address(0, 0)
        Cancel = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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