Solved

2 workbooks but vba routine to keep current workbook open but close saveas copy

Posted on 2014-11-25
9
315 Views
Last Modified: 2014-11-26
I have a vba routine that is not working properly.

I have a current workbook:    I need to keep open
I have a second workbook i'm opening...getting additional string for saveas and then I just need to close the saveas workbook.

Current workbook "MyExcel1.xlsx"

In the code below i'm opening another workbook , doing a SaveAs and Then closing it.
but keeping my current workbook open.

THIS CODE IS CLOSING MY CURRENT WORKBOOK ALSO ?


Public Sub CmeSaveAs()

Dim oldwkbk As String
Dim excelwrk As Excel.Application
Dim newdate As String
Dim sInput As String



Set excelwrk = Excel.Application
  excelwrk.Workbooks.Open "C:\Program Files\Cme\Template_DoNot_Open\Export_Cme_Template.xlsx"
  excelwrk.Visible = False

 sInput = InputBox("Enter File name here")
' saveas and then close please
    excelwrk.ActiveWorkbook.SaveAs "C:\Program Files\Cme\Cme_Export\" & sInput & "_" & newdate
excelwrk.ActiveWorkbook.Close
End Sub

Open in new window



Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 5
  • 3
9 Comments
 
LVL 26

Accepted Solution

by:
ProfessorJimJam earned 250 total points
ID: 40464787
change the line

ActiveWorkbook.SaveAs

Open in new window

 to  
ActiveWorkbook.SaveCopyAs

Open in new window


also remove the line   activeworkbook.clsoe
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 250 total points
ID: 40464797
You should use these to variables:

Dim currentwb As Workbook
Dim NewBook As Workbook

Assign currentwb to Current workbook "MyExcel1.xlsx"

then open the other one using
Set newbook = excelwrk.Workbooks.Open "C:\Program Files\Cme\Template_DoNot_Open\Export_Cme_Template.xlsx"

Now you have these seperated and closing and opening and saving is much more clear!

In this case, NewBook.SaveAs and NewBook.Close

As you can see, you fixed currentwb and newbook. With Activeworkbook, it keeps changing almost out of your control (while possible, it's much harder to keep track of).
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40464799
my current workbook is not visible ?  but it is in processes tab? showing as open ?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:fordraiders
ID: 40464831
kimcomputer,
error on this line ?
Set newbook = excelwrk.Workbooks.Open "C:\Program Files\Cme\Template_DoNot_Open\Export_Cme_Template.xlsx"
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40464835
professorjim,
Current workbook. is still closing..
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40464845
Set newbook = excelwrk.Workbooks.Open("C:\Program Files\Cme\Template_DoNot_Open\Export_Cme_Template.xlsx")
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40464862
ok,

profesor jim,

I commented out
'  excelwrk.Visible = False

'added
    excelwrk.ActiveWorkbook.Close "C:\Program Files\Cme\Cme_Export\" & sInput & "_" & newdate



now it works.?
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40465478
Thanks to both !
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40466381
Thanks for the points.  Though a bit sad you didn't apply the code, as not only do you make less mistakes (using clearly defined workbooks as opposed to activeworkbook), it's also easier to read the code much later when you forgot about it (or someone else reads it).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question