+ Reply to Thread
Results 1 to 4 of 4

VBA - mandatory cells in excel

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2016
    Location
    USA
    MS-Off Ver
    2016
    Posts
    44

    VBA - mandatory cells in excel

    Good morning!!

    I have an excel document that I would like to have mandatory cells in a specific sheet with specific cells. Below is what I've come up with.. but it does not work. The range is the cells that I need to be mandatory. Ideally I would like to have separate VBA code separate to denote in the messages box what is missing rater then it say Please field out required fields. Any help would be great!! Thank you in advance.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Sheets("Sheet1").Range("B7,B8,B9,B10,B11,B13,B14,B15,B16,B18,B19,B20").Value = "" Then
    MsgBox "Please field out required fields!"
    Cancel = True 'cancels the save event
    Exit Sub
    End If


    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: VBA - mandatory cells in excel

    1. You should place the code in standard form: Select it and click on the # icon

    2. This may be what you are looking for…

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
       Dim msg As String
       Dim mandatoryCell As Range
    
       With Sheets("Sheet1")
          For Each mandatoryCell In Range("B7:B16", "B18:B20")
             If mandatoryCell.Value = "" Then
                msg = msg + mandatoryCell.Address & "  "
             End If
          Next
       End With
    
       If msg = "" Then Exit Sub
          ' otherwise
       msg = Replace(msg, "$", "")  ' remocve $ frpm addresses
       MsgBox "Please fill out these required fields on Sheet1:  " & vbCrLf & msg
       Cancel = True 'cancels the save event
    
    End Sub
    If this has been helpful, please click on the star at the left.

  3. #3
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: VBA - mandatory cells in excel

    1. You should place the code in standard form: Select it and click on the # icon

    2. This may be what you are looking for…

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
       Dim msg As String
       Dim mandatoryCell As Range
    
       With Sheets("Sheet1")
          For Each mandatoryCell In Range("B7:B11, B13:B16, B18:B20")
             If mandatoryCell.Value = "" Then
                msg = msg + mandatoryCell.Address & "  "
             End If
          Next
       End With
    
       If msg = "" Then Exit Sub
          ' otherwise
       msg = Replace(msg, "$", "")      ' remove $ symbols from addresses
       MsgBox "Please fill out these required fields on Sheet1:" & vbCrLf & msg
       Cancel = True     'cancels the save event
    
    End Sub

  4. #4
    Registered User
    Join Date
    07-20-2016
    Location
    USA
    MS-Off Ver
    2016
    Posts
    44

    Re: VBA - mandatory cells in excel

    StuCram - thank you oh so very much! This is what I needed!!! I have been struggling with this since Friday! Thank you for being a rockstar!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need VBA code for mandatory cells in excel
    By Defect in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-19-2016, 08:34 PM
  2. How can I make multiple cells mandatory if new rows are used with some cells left blank?
    By markbarnett in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2015, 07:06 AM
  3. Macro to make multiple cells mandatory - Excel 2007
    By dupes420 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2014, 05:38 AM
  4. Mandatory field required message when user skips mandatory fields
    By Bharathi27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2013, 05:12 AM
  5. Mandatory Cells
    By MYQ in forum Excel General
    Replies: 7
    Last Post: 10-12-2012, 02:16 PM
  6. Mandatory cells
    By john.mk in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-25-2010, 11:03 PM
  7. Mandatory Cells
    By Mel1221 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-05-2006, 05:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1