VBA so it wont save if shaded cells are empty

Godismyking

New Member
Joined
Jun 25, 2018
Messages
2
Hey Folks I am running the following code so all shaded cells have to be filled out otherwise it won't let me save the excel file and so far I have this but seems not to work as it only works on some cells and only if some of that particular cell are selected and it is because of the For condition see below in bold. How do I condition the "For" so It will look only for the shaded boxes? So what I want is I have a form that needs to be filled out. The user has to fill out ALL shaded boxes if any of the shaded boxes are left incomplete it will not save.

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Step 1: Check to see if Cells are blank


Dim Var1 As String
Dim Var2 As String
Dim rCell As Range
Dim lColor As Long
lColor = RGB(242, 242, 242)
Var1 = Range("B7")
Var2 = Range("B8")


   ' This is the part I am stuck on as I do not know how to code for it to identify all shaded cells only
   
For Each rCell In Selection       
If rCell.Interior.Color = lColor Then
If rCell.Value = "" Then


      
'Step 2: Blank: cancel the Close and tell the user
        Cancel = True
     
        
    MsgBox (Var1 + " " & "please complete all shaded boxes so CSS can quickly process your order for" + " " & Var2 + "")
        'Step 3: Not Blank; Save and Close
        
            Else
        SaveChanges = True
     
      
    End If
    
    End If
    
    Next


    
    
    
   
    
    
End Sub
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you know the address of the shaded cells, why don’t you test them one by one?

if the user needs to interact with the form, why not force the user to enter the data in the form. Much easier!
 
Upvote 0
What sheet or sheets contain the shaded cells?
 
Upvote 0
Does this work? Change the sheet name (in red font) to suit.
Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Step 1: Check to see if Cells are blank
Dim Var1 As String
Dim Var2 As String
Dim rCell As Range
Dim lColor As Long
lColor = RGB(242, 242, 242)
Var1 = Range("B7")
Var2 = Range("B8")
For Each rCell In Sheets("Sheet6").UsedRange  'Change Sheet name to suit
    If rCell.Interior.Color = lColor And rCell.Value = "" Then
        Cancel = True
        MsgBox (Var1 + " " & "please complete all shaded boxes so CSS can quickly process your order for" + " " & Var2 + "")
        Exit Sub
    End If
Next rCell
        'Step 3: Not Blank; Save and Close
        SaveChanges = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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