Results 1 to 7 of 7

Thread: Save Workbook Question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Save Workbook Question

    Hi,

    I'm after a way to save my workbook without saving one sheet.
    The sheet may or may not exist (depending on user choice) but if loaded
    I don't want it to become part of the main workbook.
    I thought I could just unload it before saving, and reload it after the save but the time taken is excessive.

    And if it's not possible, please let me know and I'll try to figure out something else.

    Thanks, ABB

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Save Workbook Question

    how are you doing now?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Re: Save Workbook Question

    Doing the unload, do you mean ??

    Code:
    Set wb1 = ActiveWorkbook
        If SheetLoaded = True Then
         Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\Book2.xls")
         Application.DisplayAlerts = False
         wb2.Sheets("Sheet2").Delete
         wb1.Sheets("Sheet2").Copy After:=wb2.Sheets(wb2.Sheets.Count)
         wb2.Close SaveChanges:=True
        End If
        
        On Error Resume Next
        wb1.Sheets("Sheet2").Delete
        On Error GoTo 0
    Set wb2 = Nothing
    Set wb1 = Nothing
    
    Function SheetLoaded() As Boolean
    On Error GoTo err_NoSheet
    With Worksheets("Sheet2")
    End With
    SheetLoaded = True
    Exit Function
    err_NoSheet:
    End Function

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Save Workbook Question

    I thought I could just unload it before saving, and reload it after the save but the time taken is excessive.
    the code does not appear to do quite what is requested in the original post

    you can try like this, as it does not open any workbooks, should be quicker
    vb Code:
    1. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    2. Dim myarr As Variant, s As Worksheet
    3.  
    4.     For Each s In ThisWorkbook.Sheets
    5.         If s.Name = "Sheet2" Then
    6.             Application.DisplayAlerts = False
    7.             myarr = s.UsedRange
    8.             s.Delete
    9.             Exit For
    10.         End If
    11.     Next
    12.     Cancel = True
    13.     ThisWorkbook.Save
    14.     If Not VarType(myarr) = 0 Then
    15.         Set s = ThisWorkbook.Sheets.Add
    16.         s.Name = "Sheet2"
    17.         s.Range(s.Cells(1, 1), s.Cells(UBound(myarr, 1), UBound(myarr, 2))) = myarr
    18.     End If
    19. End Sub
    test thoroughly
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Re: Save Workbook Question

    Thanks Westconn. Played around with this and some great stuff Koolsid gave me a few weeks ago, but I think the Workbooks are too big and the time taken overall spoils things. I can see your routine would work quite nicely - apart from losing the sheet formatting. I think now my whole idea with loading a conditional sheet as required just isn't viable

    Still, interesting to try and thnks for the help

    Regards, ABB

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Save Workbook Question

    if you do not want to save it, what does it matter about the formatting?

    you could do a save as without the sheet2, then some file renaming
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Re: Save Workbook Question

    >if you do not want to save it, what does it matter about the formatting?

    Ideally it should remain just as before Saving i.e. continue as usable and with formatting.
    Yes it was possible to save the rest of the workbook excluding this sheet, but the two outcomes (a very long time with unload/reload, or lose formatting) made neither practical. Both workbooks are 25-30 megs.

    >you could do a save as without the sheet2, then some file renaming

    It's for a number of other users so would need to be simple and bullet proof !

    But thank you (and koolsid) for letting me explore the idea. It was worth trying and I learnt some new things

    Regards, ABB

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