+ Reply to Thread
Results 1 to 10 of 10

Close the source workbook after copying

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82

    Close the source workbook after copying

    Hi,

    I manage to copy a worksheet into a new workbook, but how can I change to the old one and close it without saving ?
    ActiveWorkbook.Close False
    I usually use this code, but I can't go back to the source workbook, how do I do that ?

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You need to make somesort of refernece to the source book

    Workbook("Source.xls").Close False
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    How or when do I embed that in the code ? It copies the sheet, copy and pastes just the values (to get rid of all the formulas and calculations) and then saves it to a pre-defined location with a given name. The whole code works like a treat despite being a bit patched together.
    Because if I enter the code just after the copy of the worksheet it comes up with a compiling error. (Sub or function not defined) @
    Workbook("Source.xls").Close False
    'code from http://www.erlandsendata.no/english/...filefoldername
    
    Private Type BrowseInfo ' used by the function GetFolderName
        hOwner As Long
        pidlRoot As Long
        pszDisplayName As String
        lpszTitle As String
        ulFlags As Long
        lpfn As Long
        lParam As Long
        iImage As Long
    End Type
    
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
        Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
        Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
    
    
    Sub TestGetFolderName()
       Dim FolderName As String
    '
       FolderName = GetFolderName("Bitte wählen sie den Ordner zum Abspeichern aus.")
       'Wenn kein Ordner ausgewählt kommt folgende Msg:
       If FolderName = "" Then
           MsgBox "Sie haben keinen Ordner ausgewählt."
        Else
          '(Messagebox deaktiviert) MsgBox "Die Speicher- und Druckversion wird nun in diesem Ordner generiert und abgespeichert: " & FolderName
        Sheets("Mutationsblatt").Select
          ActiveSheet.Unprotect Password:="testtest"
             Sheets("Mutationsblatt").Select
          ActiveSheet.Copy
          Columns("A:J").Select
        Selection.Copy
      SkipBlanks _
            :=False, Transpose:=False
        Columns("I:O").Select
        Application.CutCopyMode = False
        Selection.Delete Shift:=xlToLeft
        Rows("78:85").Select
        Selection.Delete Shift:=xlUp
          Cells.Select
        Selection.Interior.ColorIndex = 2
       
        Range("A7:G7,A10:G10,A16:G16,A31:G31,A40:G40,A66:G66,A68:G68,A74:G74").Select
        Selection.Interior.ColorIndex = 15
        Range("F29").Select
        
       
       'ActiveSheet.Protect Password:="testtest" (disabled temporarely)
       Range("D17").Select
     Datum.Mutationsblatt.Lieferant.xls,
           ActiveWorkbook.SaveAs Filename:=FolderName & "\" _
             & Date & "." & "Mutationsblatt." _
             & Range("A3").Value & ".xls", FileFormat:=xlNormal
          Workbook("Mutationsblatt-beta.xls").Select
          ActiveWorkbook.Close False
          End If
    End Sub
    
    Function GetFolderName(Msg As String) As String
       Dim bInfo As BrowseInfo, Path As String, r As Long
       Dim x As Long, pos As Integer
       bInfo.pidlRoot = 0& ' Root folder = Desktop
       If IsMissing(Msg) Then
           bInfo.lpszTitle = "Select a folder."
        
       Else
           bInfo.lpszTitle = Msg ' the dialog title
       End If
       bInfo.ulFlags = &H1 ' Type of directory to return
       x = SHBrowseForFolder(bInfo) ' display the dialog
    
       Path = Space$(512)
       r = SHGetPathFromIDList(ByVal x, ByVal Path)
       If r Then
           pos = InStr(Path, Chr(0))
           GetFolderName = Left(Path, pos - 1)
       Else
           GetFolderName = ""
       End If
    End Function
    I have just "assembled" most of the code and adapted it to my purpose, but like I said, works fine for my limited knowledge of VBA. I guess it comes up with the error because the code is still running.
    thanks for the help

  4. #4
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    I still haven't found a solution yet, if I try to close the workbook the code is running in it comes up with an error.

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Think mudraker just missed the s

    This worked for me

    Workbooks("Source.xls").Close False
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  6. #6
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Thanks, that's correct, there was an s missing, but the problem I have is that it doesn't execute the rest of the code properly anymore with this command. I have put the cmd at the end of the posted code, but then it doesn't save it with the defined name at the chosen location anymore. How could I make it finish the whole code and then close the old workbook ?

  7. #7
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    This saves then closes

        With Workbooks("Source")
            .Save
            .Close
       End With

  8. #8
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    No, sorry, missunderstanding: Workbook 1 opens a macro, copies a worksheet, tells it with what name and location the new workbook 2 should be saved. Workbook 1 doesn't have to be saved. (see code above)
    But it doesn't execute the rest of the code even when I put the code at the end of the whole macro.

  9. #9
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I do not fully understand what you mean by Workbook 1 opens a macro

    Is the workbook1 & the source workbook that you refer to the same workbook - If it is try this command before the End Sub command

    thisWorkbook.Close False

  10. #10
    Registered User
    Join Date
    09-18-2008
    Location
    Switzerland
    MS-Off Ver
    Office 2003
    Posts
    82
    Thanks, that works ! But strange, I have of course added the code before end sub, but it still wouldn't execute the rest of the code (like saving under a specific name)
    What's the difference between this.Workbook and Workbooks("test") if it refers to the same workbook ?
    Last edited by Simon-ch; 01-19-2009 at 03:13 AM.

+ 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