+ Reply to Thread
Results 1 to 6 of 6

VBA to check a cell equals 1 before closing

  1. #1
    Registered User
    Join Date
    05-09-2012
    Location
    Manchester, England
    MS-Off Ver
    O365
    Posts
    23

    VBA to check a cell equals 1 before closing

    Hi

    It's a long time since I have used VBA and I am very rusty, please can someone help as I think I'm going round the bend.

    I have a spreadsheet that has two sections where the user inputs data that should then add up to 100%. I want a message pop up box to appear if each section does not add up to 100% when they try to close the spreadsheet.

    For example the user will input a percentage into cells O4, R4, U4, X4, AA4, AD4, AG4 then there is a sum calculation in AH4 that adds up the cells. If it is 100% great, if not then they need to look at the input area and amend the details. There is a second input area that again the user inputs data into a range and then cell AX4 adds up the cells in that section. So AH4 and AX4 should add up to 100% each before you close the spreadsheet.

    I have written the below VBA but it is bringing up the message box to say that AH4 and AX4 are not 100% when currently they are.


    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    'Check to see if cell AH4 or AX4 is 100%
    If Sheets("LA_Returns").Range("AH4").Value <> 1 Or Sheets("LA_Returns").Range("AX4").Value <> 1 Then

    'If either cell is not 100%, cancel the close and tell user with the message below
    Cancel = True
    MsgBox "Error! In order to close, cells AH4 and AX4 must show 100%. Please check the input fields"

    'If both cells equals 100%, save and close
    Else
    ThisWorkbook.Close SaveChanges:=True

    'End the If and the sub procedure
    End If


    End Sub


    I have checked the code itself on vba checker and there were no issues found. But I am stuck and think I must have missed something obvious along the way. Please can anyone assist?
    Any help at all would be appreciated, thank you
    Last edited by nd2828; 10-21-2020 at 10:14 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-27-2010
    Location
    sYRIA
    MS-Off Ver
    Excel 2013
    Posts
    669

    Re: VBA to check a cell equals 1 before closing

    Hi
    Is your code is place in ThisWorkbook code? not inSheet code
    it should be

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,908

    Re: VBA to check a cell equals 1 before closing

    This macro placed in the code module for ThisWorkbook works for me:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Registered User
    Join Date
    05-09-2012
    Location
    Manchester, England
    MS-Off Ver
    O365
    Posts
    23

    Re: VBA to check a cell equals 1 before closing

    Quote Originally Posted by mohadin View Post
    Hi
    Is your code is place in ThisWorkbook code? not inSheet code
    it should be
    Hi
    Yes it was in ThisWorkbook code.

  5. #5
    Registered User
    Join Date
    05-09-2012
    Location
    Manchester, England
    MS-Off Ver
    O365
    Posts
    23

    Re: VBA to check a cell equals 1 before closing

    Thanks for checking

    I have gone back into the workbook, and haven't changed anything, tried to close and it is working so not sure what was going on. Thanks for your help

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,908

    Re: VBA to check a cell equals 1 before closing

    You are very welcome.

+ 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] How to I check if cell equals 48 ?
    By davi65 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-28-2016, 09:22 AM
  2. [SOLVED] Need to check the value in a cell before closing
    By SMILE in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2016, 02:30 AM
  3. Code to check in cell equals a value, if true copy row.
    By creekybones in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2014, 04:10 PM
  4. Check if a filename contains (not equals) the value of a cell
    By kevv in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2013, 10:27 AM
  5. Macros to check if any cell value has changed before closing workbook
    By sasmit0905 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2013, 09:14 AM
  6. Replies: 4
    Last Post: 08-29-2012, 02:45 PM
  7. How to check cell isnt blank upon saving or closing
    By jchamber00 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-29-2012, 04:42 AM

Tags for this Thread

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