Export into a copied file - excel

I will first tell you what I want to do, maybe that makes finding a solution easier. I want to export a file (after changing it) into a file that uses macro buttons etc on it's own. The problem is I cant delete said file.
My current solution is as follows: I use a "open file" Dialog so the user tells me the Excel that should be used as a template. VBA then should copy this file (the destination is once again given by a save file dialog) and then delete the used range in the new file and copy the values in this new file.
So there are 3 files Involved.
A: Current file with changed data and the macro I am doing right now.
B: Template File with macros.
C: New File that should be created with the macros from B and the
Data from A
So far I have this code that opens B and and copies it (i guess) it. My primary issue is how do I access the newly created file now. I assume I can somehow store it as a variable. Something like X = Path the user just selected in the save file dialog?
The deleting and inserting Data afterwards should be rather simple I guess/hope.
I hope you guys can help me and thanks a lot in advance :)
Here is my code so far:
Dim StandardPfad As String
Dim ExportBlatt As Worksheet
Dim NeueDatei As Workbook
StandardPfad = "C:\XYZ"
' ExportBlatt = Worksheets("Blatt1")
Set NeueDatei = Workbooks.Add
ThisWorkbook.Sheets("Blatt1").Copy Before:=NeueDatei.Sheets(1)
Application.DisplayAlerts = False
Sheets(2).Delete
Application.DisplayAlerts = True
' displays the save file dialog
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Vorlagen Datei auswählen", _
InitialFileName:="")
NeueDatei.SaveCopyAs Filename:="C:\Test\CopyBook.xls" 'Hier Speicherort angeben
NeueDatei.Close savechanges:=False

Either I`m missing something in your question, or... Isn't this StandardPfad enough?
It needs a bit more error handling if the user cancel's the selection, but that's your X = Path the user just selected in the save file dialog
' displays the save file dialog
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Vorlagen Datei auswählen", _
InitialFileName:="")
NeueDatei.SaveCopyAs Filename:=StandardPfad

Related

Using VBA to browse to import data beneath existing data in Excelsheet [duplicate]

I am trying to open an excel file in a folder of excel files using VBA. I direct my code to take the end user straight to the folder and allow him to choose the file from the dialog box. But I am not able to open the file even after selecting it from the dialog box.
My understanding of the problem is - I am missing out on the Command to open the file after selecting it.
Here is my Code,
thisYear = Year(Date)
'change the display name of the open file dialog
Application.FileDialog(msoFileDialogOpen).Title = _
"Select Input Report"
'Remove all other filters
Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
"Excel Files Only", "*.xls*")
'Select the start folder
Application.FileDialog(msoFileDialogOpen _
).InitialFileName = "\\driveA\Reports\" & thisYear & ""
Please kindly share your thoughts. Thanks.
I assume that you only let selecting one file (i.e. AllowMultiSelect = False).
Dim file As String
Dim myWbk As Workbook
file = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Set myWbk = Workbooks.Open(file)
First line gets the path of the selected file and then second line opens it. Add this to the end of your code.

VBA Excel save .xlsx file with the flexible name

I would like to save my file with the flexible name, which will change as the cell value changes.
The one answer is here:
Save a file with a name that corresponds to a cell value
however, I want also some fixed part of the name, which won't change unlike the part described in the query above.
Basing on this solution I tried to write something as follows:
Sub Save ()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = "NBU" & name & "- Opportunity list.xlsx"
ActiveWorkbook.SaveAs Filename:=Custom_Name
In the effect, I am getting an error:
This extension cannot be used with the selected file type. Change the file extension in the File name text box or select a different type file by changing the Save as type.
I would like to have this file in the .xlsx extension.
Excel VBA - save as with .xlsx extension
The answer above doesn't really match to my situation.
It will be vital to remove all form control buttons in the newly saved file, when possible.
Thanks & Regards,
End Sub
There is no action in the routine listed to save the file. It just simply takes the contents of a cell and creates a string with wrapped values.
I am not totally sure of what your goal is, but you need to add the action from the second link you provided. Workbook.SaveAs Method.
See the code below for a working example that I created to test.
Public Sub Save()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = ThisWorkbook.Path & "\" & "NBU" & name & " - Opportunity list.xlsx"
'Disable alert when saving
Application.DisplayAlerts = False
'Save the workbook.
ActiveWorkbook.SaveAs Filename:=Custom_Name, FileFormat:=51
End Sub
You should note that after this code has executed, you will now be in the newly created file. This is not an export.
Test this and let me know if you have any questions. There are a few things that seem to be unnecessary in your code, but we can address those if you find this answers your first issue.
Edit:
I would also call out specifically then worksheet with the range as well.
name = Worksheets("Sheet1").Range("A2")
You said the file name is 'Opportunity v1.0.xslm'. Would it have macros? My version of Excel complains about using .xlsx if there is code in the workbook.

Reusing /copying Macros from a file to another file

I have an issue where I take data from multiple files and copy it into my current file. I work with the data and now I need to create a new File that contains the macros and the "style" from one of the import files and the new data I created.
So basically I take a file, delete all data from it. Then take my data and paste it into the file. Later I save the combinations of macros from old file and new data into a new file with which is created with the name "save as dialog."
So far I have a working import for all of the files and also my work on the data is doing great. Only this export is missing.
My idea would be to make the Path of the file public during the import and then during the import file use this path to do the following:
copy this file,
delete all data besides the macros and buttons
copy my data into it.
open the "save as dialog and let the user choose a name and the location where to save it.
However, I have absolutely no idea how something like this could be done in terms of code. Some code examples with explanations would be all I need I hope that I can then tailor it to fit my needs.
This is the code I tried so far but it's just a regular "save my current file" that doesn't include any macros from another file or takes another file as basis.
Sub DateiSpeichern()
Dim StandardPfad As String
Dim ExportBlatt As Worksheet
Dim NeueDatei As Workbook
StandardPfad = "C:\X"
Set NeueDatei = Workbooks.Add
ThisWorkbook.Sheets("Test").Copy Before:=NeueDatei.Sheets(1)
Application.DisplayAlerts = False
Sheets(2).Delete
Application.DisplayAlerts = True
'displays the save file dialog
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Datei speichern", _
InitialFileName:="")
NeueDatei.SaveCopyAs Filename:=StandardPfad
NeueDatei.Close savechanges:=False
End Sub
Hello,
here is my first attempt of coding it. It doesnt work but i hope I got atleast the right idea.
Sub DateiSpeichern()
Dim StandardPfad As String
Dim ExportBlatt As Worksheet
Dim NeueDatei As Workbook
Dim VorlagenDatei As Workbook
'Sets a default directory
StandardPfad = "C:\Test"
'opens the dialog to choose the template file.
Set VorlagenDatei = Workbooks.Open(StandardPfad)
StandardPfad = Application.GetSaveAsFilename(FileFilter:= _
"Exceldateien (*.xlsx), *.xlsx", Title:="Datei speichern", _
InitialFileName:="")
'copies the selected file (something is wrong here i would assume)
VorlagenDatei.SaveCopyAs Filename:=StandardPfad
'copies my worksheet (Test) into the newly generated (copied) file. Here I need to somehow specifie the range where it is copied to.
ThisWorkbook.Sheets("Tires").Copy Before:=NeueDatei.Sheets(1)

Workbook.save - "the following features cannot be saved in macro-free workbooks..."

My sub opens up a .xlsm file in a new Excel instance, makes changes to the file, and saves the changes.
However, despite the fact that the file being opened is .xlsm, upon workbook.save I get the alert message "the following features cannot be saved in macro-free workbooks..."
This doesn't make sense to me because the file is .xlsm. Any ideas?
Edit: I believe I found the source of the problem. Although the variable "path" contains C:\file.xlsm, wkb.path is empty. (see Debug.print's below). Why is the wkb.path empty?
Set XL = New Excel.Application
XL.Visible = False
For Each Path In XLSMPaths
Set wkb = XL.Workbooks.Add(Path)
Debug.Print Path ' "C:\file.xlsm"
Debug.Print wkb.path ' ""
Debug.print wkb.name ' "file"
wkb.Save '<- alert message "The following features cannot be saved in macro-free workbooks..."
Next Path
By default, the .save method will save the file as .xls or .xlsx (dependant on your version of excel), you need to force this with .saveas
wkb.SaveAs Filename:=MyNewPathFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
obviously change the variable 'MyNewPathFilename' to whatever you want to save the file as, probably want to take the Path and check it ends in .xlsm then pass it into this variable

VBA data transfer from one workbook to another, data type mismatch error

I'm new to VBA, but learning. I've written most of the following code myself, some of it was inherited. My goal here is to loop through multiple text files (these each contain a unique set of raw data) and copy (or in some other way transfer) that data into an analysis template that I've made which will then be "saved as" with the same filename as the raw data text file. I've been working on this for several days and have done a significant amount of searching to get this far, however, I'm currently stuck with a "Run-time type '13' error - mismatch data type" that I don't understand so I don't know how to get past it. The error is # "Data.Sheets(Sheet1).Range("A1:G180000").Copy. If I comment out the aforementioned line and the one that follows it and use the line above ("Template.Sheets(Sheet1).Range("A1:G180000").Value...") I still get the same error. My code is posted below and any help is very much appreciated. Thanks :)
Sub Shift_Load_Data_Plotter_Template()
'Josh Smith
'12/27/2013
'Shift Load Data Plotter Template
'This macro will bring up the Open dialog box so you can open multiple text files and analyze them using the Shift Load Data Plotter Template
'Brings up the Open window so you can select multiple excel files to import
Dim fn As Variant, f As Integer
Dim FileName As String
'Data is the source workbook and Template is the destination workbook
Dim Data As Workbook
Dim Template As Workbook
fn = Application.GetOpenFilename("Text files,*.txt", _
1, "Select One Or More Files To Open", , True)
If TypeName(fn) = "Boolean" Then Exit Sub
'the line below was modified from from just "workbooks.open "Z:\..." to "Set Template = Workbooks.open..."
'opens the Shift Load Data Analyzer Template workbook and sets the "Template" variable equal to said workbook
Set Template = Workbooks.Open("Z:\General Reference, Tools\Shift Load Data Analyzer Template.xlsx")
For f = 1 To UBound(fn)
'the line below was modified from just "workbooks.open fn(f)" to what it shows now
'sets the "Data" variable equal to the workbook which contains the data from the text file
Set Data = Workbooks.Open(fn(f))
FileName = ActiveWorkbook.Name
'Data.Activate
'Template.Sheets(Sheet1).Range("A1:G180000").Value = Data.Sheets(Sheet1).Range("A1:G180000").Value
Data.Sheets(Sheet1).Range("A1:G180000").Copy
Template.Sheets(Sheet1).Range("A1").PasteSpecial (xlPasteValues)
'the line below used to be "ActiveWorkbook.SaveAs..."
Template.SaveAs FileName:="Z:\" & FileName & ".xlsx"
Data.Close
Next f
End Sub
The line:
Data.Sheets(Sheet1).Range("A1:G180000").Copy
Should probably read as follows:
Data.Sheets("Sheet1").Range("A1:G180000").Copy
You need quotation marks around the sheet name if you're referring to the name (the Sheets() function is looking for the sheet name you see on the tab in Excel, not the Sheet1, Sheet2, Sheet3, etc. you see in the VBA screen). Otherwise you could write it like this:
Data.Sheet1.Range("A1:G180000").Copy
Try changing it to:
Data.Sheets(1).Range("A1:G180000").Copy
Template.Sheets(1).Range("A1").PasteSpecial (xlPasteValues)

Resources