Results 1 to 19 of 19

Thread: save on Sheet Close

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Arrow save on Sheet Close

    Hi Experts,
    On closing an excel sheet the excel provoid the mgs;
    do u want to save the change in file.xls
    YES NO CANCLE
    i wana the write some code on YES click
    PLZ HELP

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: save on Sheet Close

    what code you want to write? what is your need?

    Please explain little more. so that we can understand better to help you.
    CS

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    i want to save the cells value to the database and sheet On YES click

  4. #4
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: save on Sheet Close

    I believe what you are looking for is the BeforeSave workbook event. Place this on your workbook code sheet and modify the stuff in the middle:

    VB Code:
    1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    2. Dim iReply As Integer
    3.    If SaveAsUI = True Then
    4.       iReply = MsgBox("Sorry, you are not allowed to save this Workbook as another name. " _
    5.                      & "Do you wish to save this workbook.", vbQuestion + vbOKCancel)
    6.      Cancel = (iReply = vbCancel)
    7.    If Cancel = False Then Me.Save
    8.      Cancel = True
    9.   End If
    10. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    sorry i am not looking for it
    i want the code on ,when MS-Excel give the Message on sheet/book close ,on YES

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: save on Sheet Close

    I haven't tested, but I think the only code you need in the Sub from VBAhack's example is this:
    VB Code:
    1. Me.Save
    ..however, you will probably want to put it into the BeforeClose event instead of BeforeSave.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: save on Sheet Close

    VB Code:
    1. ThisWorkbook.Save
    Works too but VBAHacks code is making sure its not "Save As" as to retain the original workbook name.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    sorry i am not looking for it
    i want the code on ,when MS-Excel give the Message box on sheet/book close
    DO you want to save the changes made in .xls file
    YES NO Cancle

    i wana code on only cliking 'YES'

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: save on Sheet Close

    See post #4.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: save on Sheet Close

    Quote Originally Posted by pandey_om
    sorry i am not looking for it
    i want the code on ,when MS-Excel give the Message box on sheet/book close
    DO you want to save the changes made in .xls file
    YES NO Cancle

    i wana code on only cliking 'YES'
    We are not sure what you are looking for?

    Do you want to click "Yes" buton?

    or

    Do you want to save some values to database if the user clicks the "Yes" button?
    CS

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    i want to save some values to database if the user clicks the "Yes" button?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    the steps are
    1)open work sheet
    2)write data on sheet
    3)on closing sheet
    MS-Excel Messg "DO you want to save chang...."
    YES , NO, Cancel
    if i click YES the data must be save, not on click NO and Cancle

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: save on Sheet Close

    VB Code:
    1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    2.     If Cancel = False And SaveAsUI = False Then
    3.         Me.Save
    4.         Cancel = True
    5.         'Add code to save data to database here:
    6.         '
    7.     End If
    8. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    i think it will work for me
    thankx u

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    on clicking "YES"
    1) Data are saved to database(Working Fine)
    But 2) worksheet not close and MS-Excel Msg remainnig

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    3) worksheet not saved
    its working fine for Excel closed but not for sheet closed

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: save on Sheet Close

    How could Excel close but not a sheet?

    Take the "Cancel = True" out.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    thanks for your support
    all working fine but one problem is that when i click YES ,in database records saved two times.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    73

    Re: save on Sheet Close

    i think problem is due to ME.Save or any

Posting Permissions

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



Click Here to Expand Forum to Full Width