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
Related
Can someone help explain why when trying to save or copy files over to a file structure that is part of a SharePoint site the code only seems to work if I use the SaveAs code once everytime I log on to my network
The code also works on other excel spreadsheets
If Dir("//teamspace.healthcare.siemens.com/content/90002613/Documents/" & Filename & "", vbDirectory) = "" Then
MkDir ("//teamspace.healthcare.siemens.com/content/90002613/Documents/" & Filename & "")
Else
The code I need to run once on logon is
With ActiveWorkbook
Application.ActiveWorkbook.SaveAs Filename:="https://teamspace.healthcare.siemens.com/content/90002613/Documents/Budget_Presentations/SavedFileName.xlsm"
Application.ActiveWorkbook.Close False
End With
I have tried multiple things, like using different directories, turning the slashes round, using variables. Nothing seems to work, even though exact same code has worked multiple times on a different spreadsheet
Thank You
If you are going to save it as a macro enabled workbook, then you need to explicitly tell the SaveAs command that. Like this:
Application.ActiveWorkbook.SaveAs Filename:="https://teamspace.healthcare.siemens.com/content/90002613/Documents/Budget_Presentations/SavedFileName.xlsm", XlFileFormat:=xlOpenXMLWorkbookMacroEnabled
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
What i am about to ask may have already been asked before but i couldn't find the relevant thread, most likely because i am not phrasing the search correctly.
What i want to do in vba is open a workbook in the same folder as the excel-workbook with the macro.
Currently i have referenced the exact location of the file:
wkbSource = Workbooks.Open("C:\Users\Desktop\Scorecard\E2E.xlsx")
The name of the excel file may not be the same in the future.
So what i want to do is have the user type in the name of the file in Sheet1!A1 cell and then use the text typed in the cell.
I tried using the following but that didnt work. Its throwing a Run-time error '76' path not found.
wkbSource = Workbooks.Open(ThisWorkbook.Path\Range("A1"))
Regards
The concatenation operator in VBA is &. Use it to connect strings together:
wkbSource = Workbooks.Open(ThisWorkbook.Path & "\" & Range("A1"))
path = "C:\Users\Desktop\Scorecard" & "\"
Name = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
wkbSource = path & Name & ".xlsx"
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.
I wrote the following code so that when an Excel spreadsheet is closed it will update its name with the current date and time:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Name = "Name_Last Opened-" & Format(Date, "MM-DD-YYYY") & _
"_" & Format(Time, "HH.MM") & ".xls" Then
Else
ThisWorkbook.SaveAs Filename:="\\C:\... Name_Last Opened-" & _
Format(Date, "MM-DD-YYYY") & "_" & Format(Time, "HH.MM") & ".xls"
FName = Sheets("Name").Range("D1").Text
Kill FName
End If
End Sub
Private Sub Workbook_Open()
Range("A1").Select
ActiveCell.FormulaR1C1 = ThisWorkbook.Name
End Sub
Additionally, the code is located within VBAProject(Name of file), under MS Excel Object - ThisWorkbook.
This code works perfectly for me or the workstation that it was created on; however, it does not execute for anyone who opens it on their worstation. Would anyone know how to get the code to execute whenever the spreadsheet is opened and closed from any computer, not just mine?
Thank you,
DFM
It's possible that Excel's security settings aren't allowing other people's computers to run the script that could be interpreted as risky malware. Perhaps you changed your security settings so long ago that you forgot about it. See if you can modify another user's security settings to see if that will make the macro execute on the workbook close.
"Would anyone know how to get the code to execute whenever the spreadsheet is opened and closed from any computer, not just mine?"
I don't think it can be done with 100% certainty unless you can ensure that every possible user will have macro security set such that your macro can execute.
Assuming you can get past that one, you should perhaps check that the users all have the worksheet in the same hard-coded path on C:\ that you seem to be using. What happens if they open the workbook from a different location?
Also:
FName = Sheets("Name").Range("D1").Text
is getting a value from one place and
Range("A1").Select
ActiveCell.FormulaR1C1 = ThisWorkbook.Name
is putting it in another.
I think I'd try something like the following (which assumes from your code that you actually only want to change the file name if it has not changed since the minute of the current time changed):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim dateTime As String
Dim oldPath As String
Dim newPath As String
dateTime = Format(Now, "MM-DD-YYYY_HH.MM") ' Format the while thing in one string - once
With ThisWorkbook
oldPath = .FullName ' what is it called now, and where did it come from?
newPath = .Path & "\" & "Name_Last Opened-" & dateTime & ".xls" ' what should it be called now?
If oldPath <> newPath Then ' only do something if not saved in last minute - is that what you really want?
.SaveAs Filename:=newPath
Kill oldPath
End If
End With
End Sub
Date() function needs administrator access to run.. so if your user is a non admin, then it will fail. Instead use now(). Most of the times this is some thing which we usually forget as we(people developing the tool) have admin access over our PC's
Fundamentally, you cannot ensure that all users will a) have a macro security setting of low or medium, and b) if set to medium, enable them when the file is opened.
Creating your own certificate would seem like the obvious answer, but in practice I find that the resultant messages and warnings are even more confusing/frightening for some end users, leading to much the same situation as with macro security. Third-party certificates avoid this, but are $$$ and almost surely overkill for an Excel workbook in a corporate environment.
What I've done where I need users to have VBA enabled is to set all sheets to xlveryhidden on save, except a custom locked sheet that only has a note saying macros must be enabled and a brief guide on how to do this. This sheet is hidden and the others restored by the workbook's workbook_open procedure, something that of course will not fire if VBA is disabled.