Solved

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

Posted on 2014-11-25
9
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

751 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