Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:12:30+00:00 2026-06-14T18:12:30+00:00

I have a form on an Excel sheet that has two mandatory cells which

  • 0

I have a form on an Excel sheet that has two mandatory cells which are often left incompleted by users. I have the following code which won’t allow the user to save the sheet if the cells are not completed, will highlight them in red and display a message box:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

ok As Boolean
Dim xlSht As Worksheet
OK = False

Set xlSht = ThisWorkbook.Worksheets("Changes Form")

'Cell 1
If xlSht.Range("B13") = "" Then
    xlSht.Range("B13").Interior.Color = RGB(255, 0, 0)
    ok = True
Else
    xlSht.Range("B13").Interior.ColorIndex = xlNone
    ok = False

If xlSht.Range("E13") = "" Then
    xlSht.Range("E13").Interior.Color = RGB(255, 0, 0)
    ok = True
Else
    xlSht.Range("E13").Interior.ColorIndex = xlNone
    ok = False

End If

End If

If OK = True Then
MsgBox "Please review the highlighted cells and ensure the fields are populated."
Cancel = True
End If

End Sub

The code works however if there are no entries in both cells then it only colours cell B13. I think once the ‘ok = True’ bit of the code runs for B13, it skips the rest of the code to the end. I am unsure how to amend it so that both cells will be highlighted.

I thought about alerting the user through Data Validation however I have a listbox in both cells so I am not sure if it is still possible that way.

Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:12:32+00:00Added an answer on June 14, 2026 at 6:12 pm

    Replace your code with the below. If the first value is empty then you are missing the logic for the second. Also, no need to set to false if there is a value. Final thing I changed was your “ok” boolean to cellsNotPopulated so its more readable.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim xlSht As Worksheet
    Dim cellsNotPopulated As Boolean
    cellsNotPopulated = False
    
    Set xlSht = ThisWorkbook.Worksheets("Changes Form")
    
        With xlSht
    
            If .Range("B13") = "" Then
                .Range("B13").Interior.Color = RGB(255, 0, 0)
                cellsNotPopulated = True
            Else
                .Range("B13").Interior.ColorIndex = xlNone
            End If
    
            If .Range("E13") = "" Then
                .Range("E13").Interior.Color = RGB(255, 0, 0)
                cellsNotPopulated = True
            Else
                .Range("E13").Interior.ColorIndex = xlNone
            End If
    
        End With
    
        If cellsNotPopulated = True Then
            MsgBox "Please review the highlighted cells and ensure the fields are populated."
            Cancel = True
        End If
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an EXCEL sheet.it has various form-fields that are named as smrBgm133GallonsGross/ smrBgm167GallonsGross
I have created a macro for excel which will pop up form that contains
I have a form which spawns from an excel add-in written with VSTO. I
I have an excel spread sheet (well, hundreds of them) which I need importing
I have a huge ASPX web form which has around 100 fields which need
I have an Excel sheet on which I have created a list consisting of
There is a dynamic form which gets build based upon an excel spread sheet.
We have an asp.net c# program that reads a sheet from an Excel file
Let's say I have an embedded Excel Spreadsheet on a Microsoft Access form. I
Using Excel 2007 VBA Have a sheet Dashboard, have other sheets but some special

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.