I have this VBA:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls"
ActiveWorkbook.Close
This is supposed to save the currently active workbook or worksheet to a path and file name provided. The extension ".xls" is used so that the exported file is a Excel 97-2003 Workbook.
It saves the Workbook or Worksheet but has a folder included in them. Let's say the file name was "Master.xls", ActiveWorkbook.SaveAs works but the VBA seem to include a folder named "Master_files" next to it as if it were saved as a web file. How can I disable this?
Note that the sheet came from an online database, when I click the link to export the records into an Excel sheet it doesn't download it but instead opens it straight from web to my Excel application which is why I created this SaveAs code.
I believe you need to expressly specify the format:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls", FileFormat:=xlExcel8
Otherwise, it is saving as HTML, along with the _files, because you got it from the Web (I think). MSDN says:
For an existing file, the default format is the last file format specified
which I am guessing is HTML for your situation.
xlExcel8 is the constant to use for .xls files per Ron de Bruin's reference.
Related
I'm very new to VB, so this is probably a very easy one. I'm creating a Word document from an Excel spreadsheet, and would like the Word doc to save in the same folder location as the spreadsheet.
I'm using the code:
.SaveAs Filename:=ThisWorkbook.Path & Range("C8").Text & ".docx"
Which I though would work, but it saves it in teh directory up from the location.
I.e. the spreadsheet is in C:/User/Documents/MySpreadsheet. But the Word doc would be saved in C:/User/Documents.
I also made a message popup to display ThisWorkbook.Path which comes up with the Spreadsheet path, so I know that's right!
I also don't think I've done the naming right, as I would like it to be named the text in cell C8. But it's actually the 'Documents' folder name with the text in C8 added on.
Thanks in advance.
Activate Immediate Window (Ctrl+G) and and add this line to your code:
debug.print ThisWorkbook.Path & Range("C8").Text & ".docx"
You will see if your path is correct. In particular, if you have "\" between folder path and filename.
I want to save my current table as a text file in the same folder as the current workbook (that is open).
I use this code:
ActiveWorkbook.SaveAs ThisWorkbook.path & "\" & filename
For some reason it save the text file in the same folder as my personal.xlsb.
I use Office 2010
If this is not possible to do easily then maybe one can force Excel to open a browse window where I can pick where I want the file saved.
ThisWorkbook points to the location where the code is written. In your case personal.xlsb.
If you want to save the table in the same directory as the active workbook, use ActiveWorkbook.path instead.
You may try this
ChDir "C:\yourfolder"
ActiveWorkbook.SaveAs Filename:= _
"c:\yourfolder" & filename & ".txt", FileFormat:=xlText, _
In this example I have created a folder name "yourfolder" in my C drive. The created text file will save in C\yourfolder\filenane.txt
Hope this is helpful
I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)
I have a macro setup to automatically open/save a file that I am opening from the web. The web format is a #csv.gz format. I have code that currently just saves the file in the default location (which I have changed to c:\files). I want to write a macro that will keep the filename of the file, but change the extention to just file.xlsm. Is there a way to do this with VBA/excel? The reason while I need to change the extension is because it currently does not work with my formulas. The default save code I have just saves the file as a #csv.txt.
Is this possible?
Integrate this code into your own:
Sub SaveIt()
Dim wkb As Workbook
Set wkb = ActiveWorkbook 'change to your workbook reference
wkb.SaveAs Replace(wkb.Name, ".txt", ""), 52 'change ".txt" to ".csv" if need be
End Sub
See Excel File Type Enum for more information the 52.
I have created a sheet in vba Excel. I would like to save it the current directory, but not in absolute path, then, when this is executed somewhere else, there won't be problem.
Can somebody help ?
I am not clear exactly what your situation requires but the following may get you started. The key here is using ThisWorkbook.Path to get a relative file path:
Sub SaveToRelativePath()
Dim relativePath As String
relativePath = ThisWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=relativePath
End Sub
VBA has a CurDir keyword that will return the "current directory" as stored in Excel. I'm not sure all the things that affect the current directory, but definitely opening or saving a workbook will change it.
MyWorkbook.SaveAs CurDir & Application.PathSeparator & "MySavedWorkbook.xls"
This assumes that the sheet you want to save has never been saved and you want to define the file name in code.
If the Path is omitted the file will be saved automaticaly in the current directory.
Try something like this:
ActiveWorkbook.SaveAs "Filename.xslx"
Taking this one step further, to save a file to a relative directory, you can use the replace function. Say you have your workbook saved in: c:\property\california\sacramento\workbook.xlsx, use this to move the property to berkley:
workBookPath = Replace(ActiveWorkBook.path, "sacramento", "berkley")
myWorkbook.SaveAs(workBookPath & "\" & "newFileName.xlsx"
Only works if your file structure contains one instance of the text used to replace. YMMV.