Solved

Changing a File SaveAs parameter to a new variable

Posted on 2014-10-24
4
83 Views
Last Modified: 2014-10-28
I use the code below to save a file.  What I need is the filename to change.  I have a variable name: SalesOrderNum, and I would like to save this in a format: [SalesOrderNum]_lines.xlsx

How would I change the myFileNameO to save file correctly?  Then later in the procedure I need to open it back up.  How would in use the same myFileNameO or variable [SalesOrderNum] to reopen the file when needed?  Please advise and thanks.

myDirECTorY = "C:\temp\"        'Wherever you want
myFileNameO = myDirECTorY & Format(Now(), "yyyymmdd_hhmm") & " report" & ".xlsx"    

Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:=myFileNameO, FileFormat:= _
      xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close

Open in new window

0
Comment
Question by:RWayneH
[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
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
Steve earned 500 total points
ID: 40402689
This will save it but to reopen it you will have to get the "SalesOrderNum" the file was saved with to open it.

myDirECTorY = "C:\temp\"        'Wherever you want
myFileNameO = myDirECTorY & SalesOrderNum & "_Lines.xlsx"

Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs FileName:=myFileNameO, FileFormat:= _
      xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close

'***********************************************************

PathName = myDirECTorY
FileName = SalesOrderNum & "_Lines.xlsx"

Workbooks.Open FileName:=PathName & FileName

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40402723
Ok, thanks for the save part, however later in the procedure [SalesOrderNum] should be the same as it is when it was saved.  Perhaps I should just leave it open?   No...

How would it look to open it, assuming that [SalesOrderNum] value is the same?
0
 

Author Comment

by:RWayneH
ID: 40402913
I made a mistake in the save part, it needs to be saved as an .xml  Will it matter if I just change the .xlsx in myFileNameO to .xml?  and I will definitely need to reopen the .xml   I will need to save it as a variable and use that later in the procedure.  The variable needs to be the full file path as text.

Sample on a given time would be: C:\_SAP\Extracts\SalesOrd\200304647_Lines.XML      the 200304647 would be whatever [SalesOrderNum] is.

So the next time it could be: C:\_SAP\Extracts\SalesOrd\200304648_Lines.XML  that [SalesOrderNum] is dynamic.  Hope this makes sense
0
 

Author Closing Comment

by:RWayneH
ID: 40409797
Thanks for the help!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

623 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