+ Reply to Thread
Results 1 to 7 of 7

Macro operates on 'closed' workbook & Current Active WB

Hybrid View

  1. #1
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    Angry Macro operates on 'closed' workbook & Current Active WB

    Situation:
    Contract Template is opened, auto numbering macro is run.
    File is 'Saved As' with new name..
    SAVED Template is re-opened and used as a 'template' for new Contract
    (since changes required are minor).
    The Template is then "Saved As' with a new name
    The numbering macro is run (It increments as it should)
    Then SAVED again.

    Here comes the problem.
    On MOST occasions, if the original is now re-opened, the number has incremented in it as well.
    Also, my testing confirms that the Salesmans Name and Managers Name portion of the macro is also 'redone'.

    The only thing that I can come up with is that somehow the 'Set myBook' does not 'release' (for want of a better word).

    This problem apparently been in existence since I created the macro, but I was just made aware of it.

    If an experienced eye could review my code and let me know what the problem code is and/or how to circumvent this problem, it would be greatly appreciated.

    
    Sub QuoteNum()
    '   September 29, 2005
    '   Creates and Inserts Sequential Quotation Number on CoverPage of Contract
    
            Dim myBook As Workbook
                    '( The current selected Contract Template)
            Dim myQCNUM As Workbook
                    '(Provides Quotation #, Salesman & Managers Name for Contract)
    
            Application.ScreenUpdating = False
            Application.EnableEvents = False
    
    '   The "CoverPage" to be the active WS when initiating macro
            Set myBook = ActiveWorkbook
                Sheets("CoverPage").Activate
    '                ActiveSheet.Unprotect Password:="xxxxx"
    
            Set myQCNUM = Workbooks.Open("C:\Excel Add_Ins\QCNUM.xls")
    
    '   QCNUM is now Open (available)
    '   Go to QCNUM, Sheet #2, Reference cell G6
    '   (this is a concatenated number of 3 different cells)
    '    Go to myBook, Sheet "CoverPage", Reference cell E4
    
           myBook.Worksheets("CoverPage").Range("E4").Value = _
           myQCNUM.Worksheets("Sheet2").Range("G6").Value
    
    '   Now go to QCNUM, Sheet # 1, B6 and get Salesmans Name
    '   Go to myBook, Sheet "CoverPage", Reference cell C38
    
            myBook.Worksheets("CoverPage").Range("C38").Value = _
            myQCNUM.Worksheets("Sheet1").Range("B6").Value
    
    '   Go to QCNUM, Sheet # 1, F6 and Copy Managers Name
    '   Go to myBook, Sheet "CoverPage", Reference cell E38
    
            myBook.Worksheets("CoverPage").Range("E38").Value = _
            myQCNUM.Worksheets("Sheet1").Range("F6").Value
    
    '       myBook.ActiveSheet.Protect Password:="xxxxx"
    
    '  The following increments the number in QCNUM.XLS for use on next Contract
        '  QCNUM.xls:  Copy Cell G4,  Paste to Cell G3
        '  This copies the most recent used number (G4)into the "starting
        '  number"  cell (G3), which in turn makes increments (G4), by 1
    
            With myQCNUM.Worksheets("Sheet2")
                .Range("G3").Value = .Range("G4").Value
            End With
    
    ' Save only myBook
            With myBook
                .Save
            End With
    
     'Save and Close QCNUM.xls
            With myQCNUM
                .Save
                .Close Savechanges:=True
            End With
    
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by BEEJAY; 05-03-2011 at 03:32 PM.

  2. #2
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Macro operates on 'closed' workbook & Current Active WB

    Concider the followings:

    Add the following Code at the end of your procedure (To release te variable):
    Set myBook= Nothing
    Set myQCNUM = Nothing
    Consider using:
    Set myBook = ThisWorkbook  'Instead of using 'ActiveWorkbook'

    Very important analyze again your Saving/Opening order.

    Your macro is saving the workbook, so if you Open the workbook; run the macro and then "save it As" with another name then you end up with two identical workbooks.

  3. #3
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Macro operates on 'closed' workbook & Current Active WB

    RoyUK
    Reading thru your post several times - I get impression your quote process/procedure is substantially different from mine.
    For your information:
    16 Contract Templates, 17 Salesmen / Dealers. On everyones computer:
    All Contract Templates are Stored in C:\Contract Templates
    Custom Add-ins are Stored in C:\Excel Add_Ins
    QCNUM.XLS: Provides Sequential number, and names
    Custom Menu A: Provides quick retreival of required Contract Template
    Custom Menu B: Provides quick access to select next quote #, and a host of printing options
    The Contract Templates themself consist of:
    Cover Page; Truck Specs Sheet; Options, Selectable Sheet; Unit Specs Sheet; Notes Sheets; Warranty Sheets;
    The biggest template has Unit Specs sheet consisting of almost 2300 rows.
    I'm rather proud of how these templates have evolved over the years. The Salesmen claim it saves them hours and hours in doing quotes. HOWEVER, I am away, and would the first to admit, that there probably is a better way to do this. In the meantime, I try to read as much as I can about Excel and programming, so I can improve the 'system'.

    I greatly appreciate your thoughts and comments - Often ideas start to germinate from comments recieved, which, in time, turn into desireable upgrades to the system/process.

    Thanks again.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro operates on 'closed' workbook & Current Active WB

    Where is the macro stored?

    Baically I would have a template file (*.xlt). The code would open that & add the sequential number. In cases like this the master workbook would have the code & a summary of relevant information would be saved, i.e customer, number, sales person etc. In fact I generally don't have separate workbooks for individual quotes but use this summary to recreate quotes and/or convert to invoices based on the stored data.

    If the code were in a an addin or separate workbook the number could be stored.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Macro operates on 'closed' workbook & Current Active WB

    Wizz:
    I expect the first codes supplied will do the trick. Since the problem is intermitent on my computer, I will modify the code on my bosses computer and let him try it out.
    The second code bogs down the macro when it tries to 'copy' from one sheet to another. When I go back to 'ActiveWorkbook', it works fine again.

    Thanks so much for your input. I was not 'familiar' with = Nothing, and could not find anything in my manuals or on the .net about it.

  6. #6
    Forum Contributor
    Join Date
    08-02-2007
    Location
    Panama & Austria
    MS-Off Ver
    2003 & 2010
    Posts
    186

    Re: Macro operates on 'closed' workbook & Current Active WB

    Quote Originally Posted by BEEJAY View Post
    The second code bogs down the macro when it tries to 'copy' from one sheet to another. When I go back to 'ActiveWorkbook', it works fine again.
    Sorry, I didnt check that your code ("QuoteNum") was in a different workbook than the Template workbook ("myBook").
    'Thisworkbook' is very useful when both code and the referenced workbook are the same.

    It is a good practice to allways release your object variables (Set myVariable = Nothing) and your arrays (erase myArray).

  7. #7
    Registered User
    Join Date
    03-17-2011
    Location
    Guelph, Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Macro operates on 'closed' workbook & Current Active WB

    Quote Originally Posted by Wizz View Post
    Sorry, I didnt check that your code ("QuoteNum") was in a different workbook than the Template workbook ("myBook").
    'Thisworkbook' is very useful when both code and the referenced workbook are the same.
    That is helpful - Thank-you

    Quote Originally Posted by Wizz View Post
    It is a good practice to allways release your object variables (Set myVariable = Nothing) and your arrays (erase myArray).
    This idea is new to me, yet so logical. I'll be reviewing all my macros (lots of them) for this situation. Thank-you

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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