Solved

Changing a File SaveAs parameter to a new variable

Posted on 2014-10-24
4
65 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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 the scrolling table in Microsoft Excel using the INDEX function.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now