Consulting

Results 1 to 13 of 13

Thread: Error help BeforeClose

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Error help BeforeClose

    I have a simple code that for some reason it gives me an error once in a while.

    [VBA]Option Explicit
    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    FrmTotals.Show

    End Sub[/VBA]

    Now I have this on a spreadsheet that users input data and when closed the form comes up and gives then totals. Now I don't know if it's something the user is doing because it doesn't occur to all users.

    It gives the user a RunTime error '13' and End Button.

    When I unlock the vb and close I get the RunTime Error when I click Debug It highlights

    FrmTotals.Show

    And when i put the mouse over it it says

    FrmTotals = <Object variable or With block variable not set>

    Any Ideas?

  2. #2
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    My temporary fix is copy the data and paste it into the original template spreadsheet and save it. Which always works fine.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    FrmTotals.Show
    Cancel = True
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    will do thanks mdmack

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    mdmack that shows the form but doesn't close the workbook after form closes.

    The form is a totals form with a close button the saves the activeworkbook, then closes the form.

    I need for it to close the workbook after form closes.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Remove the line
    Cancel = True
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Yeah that's what i ended up doing. Can you see what you think xld

    I have a simple code that for some reason it gives me an error once in a while.

    [vba]


    Option Explicit Private Sub Workbook_BeforeClose(Cancel As Boolean) FrmTotals.Show End Sub [/vba]


    Now I have this on a spreadsheet that users input data and when closed the form comes up and gives then totals. Now I don't know if it's something the user is doing because it doesn't occur to all users.

    It gives the user a RunTime error '13' and End Button.

    When I unlock the vb and close I get the RunTime Error when I click Debug It highlights

    FrmTotals.Show

    And when i put the mouse over it it says

    FrmTotals = <Object variable or With block variable not set>

    Any Ideas?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    What is the code behind the form Close button?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    This is in the ThisWorkbook
    [VBA]Option Explicit
    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    FrmTotals.Show

    End Sub[/VBA]

    This behind the FrmTotals Form
    [VBA]Private Sub CmdClose_Click()
    ActiveWorkbook.Save
    Unload FrmTotals
    End Sub
    Private Sub UserForm_Initialize()
    FrmTotals.LblTotalDepot.Caption = Range("$R$1")
    FrmTotals.LblTotalVam.Caption = Range("$S$1")
    FrmTotals.LblTotalMortgage.Caption = Range("$T$1")
    FrmTotals.LblTotalServiceReq.Caption = Range("$U$1")
    FrmTotals.LblGrandTotal.Caption = Range("$V$1")

    End Sub[/VBA]

    hope that helps.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is frmTotals in the workbook that is closing, in Personal.xls or elsewhere?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Yes the FrmTotals is attached with the spreadsheet if that's what you mean. It's not an external spreadsheet with the form.
    Last edited by Emoncada; 02-15-2008 at 11:55 AM.

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't see a problem with your code, but I don't see its purpose.
    Why load a form just to save the workbook?
    The data to populate the form is not sheet specific. Does this matter? Could this cause the error?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    The Form just shows the user Total Orders by group. I have the Activeworkbook.save just to make sure it's saved before closing. Unless there is a better way you can recommend to show the user the totals and close the workbook.

Posting Permissions

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