Results 1 to 5 of 5

Thread: Having some doubts on Excel VBA Code

  1. #1
    Join Date
    Feb 2012
    Posts
    13

    Having some doubts on Excel VBA Code

    I am brand new to using VB. I have been playing around with setting up required fields in Excel. I was lucky to find a post with the following code:
    Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
    Boolean)
    If Worksheets("Sheet1").Range("A2").Value = "" Then
    Cancel = True
    MsgBox "Please Fill-In A2"
    End If
    End Sub
    I have tested this and it works fine. This basically ensures that excel can't be saved unless the specified cells are filled in. Now for a really stupid question. How do I save this script to the excel workbook so that I can distribute to other users? Obviously if I try and save the workbook itself it prompts me to enter the required fields before saving, and if I just close without saving the script is lost. Any suggestions would be appreciated.

  2. #2
    Join Date
    May 2011
    Posts
    523

    Re: Having some doubts on Excel VBA Code

    Try disabling events on your machine. In the VB editor, open the Immediate Window and type Application.EnableEvents=False. Save the workbook, then set back to true.

  3. #3
    Join Date
    Mar 2011
    Posts
    542

    Re: Having some doubts on Excel VBA Code

    Try this "password" approach.
    Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Worksheets("Sheet1").Range("A2").Value = "xxx" Then
    End
    End If
    If Worksheets("Sheet1").Range("A2").Value = "" Then
    Cancel = True
    MsgBox "Please Fill-In A2"
    End If

  4. #4
    Join Date
    Aug 2011
    Posts
    695

    Re: Having some doubts on Excel VBA Code

    Sorry, sent before finishing. I meant to add that you can change the "xxx" to be any "password" that you choose, just for the purpose of being able to save the workbook without triggering the main intent of your macro and of course you can also put that password in another cell but, I guess it gets curiouser and curious user cause then you would need a WorksheetOpen event to delete the password when a user opens the file or it would be there all the time.

  5. #5
    Join Date
    Nov 2010
    Posts
    503

    Re: Having some doubts on Excel VBA Code

    I am also new to VBA and I'm using Excel 2010. I've tried using the code on this forum and have tried several others on other forums similar to it. Each time I attempt to test if this will work the workbook saves and does not prompt me to enter any missing information.

Similar Threads

  1. Replies: 4
    Last Post: 24-02-2012, 04:17 PM
  2. EXCEL.EXE followed by code problem
    By wheres4 in forum MS Office Support
    Replies: 1
    Last Post: 29-08-2011, 12:13 PM
  3. Excel VBA Code For Minimizing UserForm
    By warehouse peon in forum Software Development
    Replies: 4
    Last Post: 25-01-2010, 08:45 PM
  4. Code execution has been interrupted in Excel
    By Vineeta in forum Windows Software
    Replies: 3
    Last Post: 04-11-2009, 12:31 PM
  5. Error in Excel VBA code
    By Unnat in forum Software Development
    Replies: 3
    Last Post: 16-10-2009, 04:51 PM

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
  •  
Page generated in 1,713,882,869.82785 seconds with 17 queries