In Excel I am writing a macro to move and format data between two files. I begin by opening the first file and running a macro that allows you to choose a file.
I am opening a file using this code:
myFileName= Application.GetOpenFilename(filefilter:="All Files, .", Title:="All Files")
I then follow it with this code:
Workbooks.Open Filename:=myFileName
Later in the code Module, I would like to copy and past things between this newly opened file and the one I ran the macro from inside. Currently I am having to hard code their names like so:
Windows("data.xlsx").Activate
How can I store the name of the newly opened file in a string and how can I get the name of the excel file I am in into a string?
Thanks
Before opening the second workbook assign the name of the current workbook to a variable:
nameFirstWorkbook = ActiveWorkbook.Name
You already get the name of the second workbook from the open file dialog.
Related
I would like to;
obtain a list of files from a specific source folder (e.g. D:\My Drive\EM SSC\PA\AttNew)
open the first source file (all Excel files, all the exact same format)
run a macro that copies certain fields from the opened file into a data table (I already have this working for a single (named) file) on a different Excel file (C:\Users\r5\Documents\DataFile_v1.xlsx)
close the source file (an ideally delete it or move it to a different folder)
open the next source file from the list
continue until all source files are dealt with
save the data table file
New Excel source files are automatically copied into the source folder each day, so I need to open each source file, copy data to my table and then remove the files, ready for the next days files.
I have only been able to complete the steps related to copying the data fields from a single source file to the data table. I am unsure of how to do the iterative part to open one file after another. I am not an experienced VBA user. I need to open a source file, copy data from it to my table, then close/delete the source file and move on to the next source file.
Use Task Scheduler to run a bat file that runs a .vbs to open and runs macros in your excel file. Explanation and example here
The bat file will contain something along the lines of
"D:\My Drive\EM SSC\PA\AttNew\Automation.vbs"
The .vbs file would be something along the lines of
Set xlsxApp = CreateObject("Excel.Application")
xlsxApp.Visible = True
Set xlsxWorkbook = xlsxApp.Workbooks.Open("D:\My Drive\EM SSC\PA\AttNew\AutomationProject.xlsm")
xlsxApp.Run("Macro1") 'Name of your macro
Closing your workbook would contain something like this, in your ThisWorkbook VBA editor
Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Closes all other
worksheets par "MainSheet", saves user time not having to delete
imported sheets everytime
For Each ws In ThisWorkbook.Worksheets
Application.DisplayAlerts = False
If ws.Name <> "MainSheet" Then 'If a worksheet is not named "MainSheet" it gets deleted
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
MsgBox ("All sheets are deleted except specific sheet - After this, you
can click either 'Save' or 'Don't Save' button") 'Message box to reasure user is okay with either option when closing the file
End Sub
I am not sure about next stages, but let me know how this works so far
Is it possible to SaveAs workbook in to a new location with macros enabled(.xlsm) without having the changes saved in old one.
In example i open original workbook, create a new sheet, then macro would save it to a new location without changing original file, so if i were to open the original file again later it is without the new sheet created before. All help is much appriciated
I am using this code to save a file, but it also saves changes in the original file.
Dim path As String
Dim name As String
path = Application.ActiveWorkbook.path
name = Replace(ActiveWorkbook.name, ".xlsm", "")
Application.ActiveWorkbook.SaveAs Filename:=path & "\" & name & "_spreadcell", FileFormat:=52
My plans for future are to import sheets from another workbook and then save this new file to opened workbook location and not save changes in the original file. So if something gets messed up, the user could still use the original file again to import and create new workbook.
Best regards
As i commented before:
That's basically what SaveAs does. It Saves File A as File B and opens File B afterwards without saving changes to File A. There's also SaveCopyAs, which saves a copy of File A as File B, but keeps File A open.
In short: I want to access the name of the .xlsm file calling an .xlam add-on, from inside the code of the add-on.
In more detail: I'm using Excel to allow people to export create data files after editing worksheets.
I first created a workbook that saved those data files with the name of the workbook followed by .data. That worked. Now I've turned that workbook as an add-on (.xlam file).
That works also, but the name of the saved data file is the name of the add-on, not the name of the xlsm file containing the xlam file.
Solved - the code now reads as follows:
(compute myOutputComputedEarlier as content to save in file)
...
fileAndPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ".data"
Open fileAndPath For Output As #1
Print #1, myOutputComputedEarlier
Close #1
Maybe you should try ActiveWorkbook instead of ThisWorkbook, but its hard to say without any code from you.
I'm working on a Macro and want to know about this to avoid any bugs or crashes.
Scenario:
I have a file which does all the processing (Macro File). My code opens a pre-existing excel (Template) file then writes some things into it and then Save As's it at particular location.
For Opening pre-existing file, I'm using this:
Set SOWbk = Workbooks.Open(FileName:=SOFileName, ReadOnly:=True)
For Saving As this file, I'm using this:
SOWbk.SaveAs FileName:=ThisWorkbook.Path & "\" & TextBox13.Value & "_SO.xlsm", FileFormat:=52
Questions:
1. After Save As, does the original file (Template) remains open and i have to close it anyhow?
2. Does "SOWbk" Object gets the reference of the newly saved as file automatically?
I tried searching this on Google and on Stack as well. I did not found any help on this. Thank you for your help in advance! Much appreciated.
Answer to Question 1: Does the old workbook stay open after SaveAs
No. You open Workbook A that is stored in Location A. When you use the SaveAs function, you save the "current Version" of the open Workbook (which is not the same as what is stored in Location A) to Location B. The file that is now open is file B so to say.
Answer to Question 2: What happens to the Workbook Object
The Object refers to what i refered to as Workbook B. The explanation is the same as above. The opened workbook is not a representation of what is stored in Location A.
I have a folder of .xls files of which some are genuine excel files and some are excel xml files (but still with the .xls extension). To work with the files in a separate program, I'm trying to convert them all to a consistent format (Excel 98-2004) and want to use an applescript to do this while retaining the original file names and directory structure.
As of now, I have a working loop and list of files, and am accomplishing everything but the save function:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
save workbook as workbook (active workbook) filename file_Name file format (Excel98to2004 file format) with overwrite
close workbooks
quit
end tell
When I run this code, I get the following replies:
tell application "Microsoft Excel"
activate
open "/Users/drewmcdonald/Desktop/Madhupur_10February2014.xls"
get active workbook
--> active workbook
save workbook as workbook (active workbook) filename "/Users/drewmcdonald/Desktop/test.xlsx"
--> missing value
close every workbook
quit
end tell
The spreadsheets are successfully opening and closing, but the file format is not changing and I don't know what to make of the "missing value" error. Any idea how to get this to work? Thanks!
EDIT: Just noticed that the file format parameter is missing from the reply text, so I'm guessing the problem is in there.
Got it.
I needed to set a workbook name variable to get full name of active workbook before trying to save it. This is because the workbook name that Excel assigns as it opens sheets is somewhat inconsistent. Final code looks like this:
set file_Name to "/Users/me/path/to/data.xls"
tell application "Microsoft Excel"
activate
open file_Name
set wkbk_name to get full name of active workbook
save workbook as workbook wkbk_name filename file_Name file format Excel98to2004 file format with overwrite
close workbooks
quit
end tell