How to save as with VBA in current location - vba

I'm currently saving my excel file with this command:
ActiveWorkbook.SaveAs Filename:="pleasework", FileFormat:=52
But when it saves it, it saves it in documents.
I want to save it in the current location of the macro (of the file, where it's activated).
Any advice? Changing it to:
Filename:="C:/pleasework"
Won't work...
Thanks!

Give the following a try
...
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\pleasework", FileFormat:=52
...
In addition you may first want to check whether Application.ActiveWorkbook.Path returns a valid path so in case your currently open file wasn't saved yet you won't try to save it to an invalid path.

Try this
Option Explicit
Sub Book_Path()
Debug.Print ThisWorkbook.Path
ActiveWorkbook.SaveAs FileName:=ThisWorkbook.Path & "\" & "pleasework", FileFormat:=52
End Sub

Related

Converting IQy files to XLSX using VBA

I currently wrote a Pyhon script that extract metadata from SharePoint online using the "Export to Excel" button. When the data saves it saves as a IQy files (internet query file) and I need to save it as an XLSX. Theres about 30 files that I want to loop through and save.
These IQy files can be opened with excel and then I can use saveas to save it as an XLSX. Is there a way to automate this? I found some code in another forum but I don't completely understand it.
ThisWorkbook.Sheets.Copy
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
ActiveWorkbook.Close
the line I don't understand is ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Date, "mm.dd.yyyy") & " " & "Position Report Ver.2.xlsx", FileFormat:=51
Would it be possible to get an explanation of this line? or is there another way to use the saveas to convert these files into XLSX? I'm open to using any other language as well.
This seems pretty clear. It's calling SaveAs to save the workbook to a particular path, using the FileFormat argument to save it as an XLSX file, which is type 51.

Save Excel table in current folder

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

Excel workbook trying to save to network directory when it shouldn't when running macro

I'm trying to get a macro to work where it saves a file a certain format and reopens it to avoid compatibility issues, however, when the macro runs it tries to save to a network directory that is no way specified in the code.
Dim wkbook As String
wkbook = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsm"
ActiveWorkbook.SaveAs wkbook, FileFormat:=52
ActiveWorkbook.Close
Workbooks.Open Filename:=wkbook
Use ChDir to set the directory.
ChDir("C:\mydirectory")

vba excel 2010 Environ("username") not working in saveas filepath

For some reason I can get the Message Box to tell me what the username is, but it doesn't work the same way within the save as path. I get a "Cannot find path" error.
Not sure how to fix this. I've also looked at references, and they seem like they are correct.
If I change the path to include the actual username per computer, the macro works fine. But I need this to be a global macro for any computer/user. Any help would be greatly appreciated.
Here is what I have:
Sub SAVEAS_2010()
'
' SAVEAS_2010 Macro
MsgBox Environ("username")
Dim UserName As String
UserName = Environ("username")
ChDir "C:\Users\" & UserName & "\Dropbox\Open Machine Schedule"
ActiveWorkbook.SaveAs FileName:= _
"C:\Users\" & UserName & "\Dropbox\Open Machine Schedule\Open Machine Schedule - Current_2.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
Seems an old question. But for anyone who stumble on this post, adding $ worked for me -
& Environ$("username") &
Or you could just put VBA. in front of it. This fixes it for me.
i.e. Environ("Username") becomes VBA.Environ("Username").
Environ("UserProfile") &
This is what worked for me on 2007-2013 Excel versions.
Just found the same problem using VBA 7.1
Solution is to use
path = Environ$("Userprofile") & "\Documents\temp_excel_files\"
Environ$("Userprofile")
Will give you something like "c:\users\Johnsmith" to put into your path.
Cheers!
Andrew

Saving an Excel sheet in a current directory with VBA

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.