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 ?
I usually use this code, but I can't go back to the source workbook, how do I do that ?![]()
ActiveWorkbook.Close False
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 ?
I usually use this code, but I can't go back to the source workbook, how do I do that ?![]()
ActiveWorkbook.Close False
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 assistedor failed to assist you
I welcome your Feedback.
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
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.![]()
'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
thanks for the help
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.
Think mudraker just missed the s
This worked for me
VBA Noob![]()
Workbooks("Source.xls").Close False
_________________________________________
![]()
![]()
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 !!!
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 ?
This saves then closes
![]()
With Workbooks("Source") .Save .Close End With
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.
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
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks