+ Reply to Thread
Results 1 to 7 of 7

Vba help - required cell value before closing excel sheet

  1. #1
    Registered User
    Join Date
    05-15-2018
    Location
    Ottawa, Ontario
    MS-Off Ver
    2016
    Posts
    3

    Question Vba help - required cell value before closing excel sheet

    Hi all,

    A little back-story - our company keeps track of purchase orders via an excel spreadsheet (each row contains: date, po. number, tech name, part name, job code number or job name, cost...). In column C we note the name of our technician, and in column G we note the job code number or job name. Unfortunately we've been having an issue with some staff not filling in the job code number/name, which is a huge issue for billing/invoicing purposes.

    I am looking for some help in writing VBA code that will make an empty cell in column G require a value before closing/ saving the workbook if a text value exists in the same row in column C. Ideally any user who has left the value in column G blank will be prompted with a message box or something similar when they try to close the workbook - and ideally they will be unable to save or close it at all unless a value exists in that G cell.

    This is the first time I've attempted using VBA, as I often just stick with conditions and if statements. I have included what i have written so far, but even that took about half an hour

    Any help you may be able to offer would be amazing and super appreciated.

    Cheers!

    Kym




    Function reqd_cell_data() As Boolean

    If (IsText(C)) And (IsEmpty(G)) Then

    MsgBox Prompt:="JOB CODE REQUIRED", Buttons:=vbOKOnly + vbInformation

    End Function




    PS, if there is also a way for the message box to include the cell that triggered the prompt (it'll change every time as the prompt is triggered) that would be FANTASTIC. (IE. "JOB CODE REQUIRED - G89")

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

    Re: Vba help - required cell value before closing excel sheet

    Try this approach. Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make an entry in any cell in column C and exit the cell. The macro will force entry into column G of that row.
    Please Login or Register  to view this content.
    Last edited by Mumps1; 05-15-2018 at 03:43 PM.
    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.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Vba help - required cell value before closing excel sheet

    Enter the following code in the Thisworkbook module.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Registered User
    Join Date
    05-15-2018
    Location
    Ottawa, Ontario
    MS-Off Ver
    2016
    Posts
    3

    Re: Vba help - required cell value before closing excel sheet

    Mumps1, this is perfect, thanks so much! Just one quick question - is there a line I can add to delay the pop-up box? typically we go in order of row/column (filling in 1C, 1D, 1E, 1F)- it would be ideal if the pop-up didnt appear until they tried to move past the job code box, in order to give staff the opportunity to enter it without prompt


    dflak - I could not get this code to run or generate a pop-up within our spreadsheet. I tested it on your attached sheet and it worked perfectly - I must be doing something incorrectly

    edit: dflak - after a little trial and error I managed to get a pop-up saying there was a bug in the code. I have attached a screen-shot of the error. Hopefully it'll work once that is cleared up!!

    Untitled1.png
    Last edited by champagnesky; 05-16-2018 at 08:55 AM.

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Vba help - required cell value before closing excel sheet

    Open the VB Editor and double-click on ThisWorkbook. That is where this code needs to be copied and pasted.

    Events have to be enabled, but unless you've turned them off somewhere else, they should come up that way.

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

    Re: Vba help - required cell value before closing excel sheet

    I'm afraid that with this type of macro, it wouldn't be possible. The macro forces entry into the box in the pop-up which then transfers the input into column G. The user doesn't have to click on a cell in column G at all.

  7. #7
    Registered User
    Join Date
    05-15-2018
    Location
    Ottawa, Ontario
    MS-Off Ver
    2016
    Posts
    3

    Re: Vba help - required cell value before closing excel sheet

    I googled it, and apparently it has to do with the name of my sheet. I tried swapping out (``sheet1``) with the name of the sheet i was using... and VOILA!

    Thank you so much for all of your help, both of you!!

+ 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. how can i make a cell mandatory before saving or closing the sheet?
    By au10tic1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2017, 03:54 PM
  2. [SOLVED] Excel VBA code required to move cursor to a sheet and cell as specified in cell A1
    By Smudge72 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2016, 05:47 PM
  3. Message before closing an Excel sheet
    By bijumon.k in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2013, 12:59 PM
  4. Two Cells required before saving or closing.
    By oparfait in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2013, 03:56 AM
  5. [SOLVED] Object required when closing a workbook
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2012, 01:35 PM
  6. Excel sheet is NOT closing (auto opening) :-(
    By excel2007 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2009, 08:40 AM
  7. Excel data validation required before saving and closing
    By excel2007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-25-2009, 08:24 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