Naming a workbook with a password - vba

I am trying to figure out how to add a password to my workbook. The code does save it when I remove the password part ("sp17"), but I get a syntax error while its there. How would I correct this error?
ActiveWorkbook.SaveAs fileLocation degreeArray(criteria) & " " & format(Date, "MMM-YY") & ".xlsx", 51, "sp17"

I made a small change and now it works. The Format was written incorrectly. Thought about deleting my question but it was hard to find out how to do this so i'll leave it for any one who needs it. This was the right way to write it:
ActiveWorkbook.SaveAs fileLocation & degreeArray(criteria) & " " & Format(Date, "MMM-YY") & ".xlsx", 51, Password:="sp17"

Related

VBA SaveCopy function + lose a worksheet

I created an Excel 2016-based template, which the user can fill and create a work form based on it. User inserts an unique ID and with basic INDEX&MATCH formulas some ID-related parameters are being fetched from separate worksheet a. The work form is created with VBA-macro using SaveCopyAs method.
After the parameters have been fetched and VBA is launched to create the work form the ID will not change anymore. Thus, I don't need the whole worksheet a anymore and would like to drop it to keep the work form more lightweight. I'm capable of retaining the fetched parameters, so this is not a problem.
I would NOT want the user to have to re-open the form every single time a work form is created, so I don't want the VBA to remove worksheet a from the template itself, as even though the user can't save changes to the template, (s)he would have to re-open the template file every time a work form has to be created.
Any idea if something could be done? Might it be possible to somehow run SaveCopyAs or similar method, but drop the worksheet a at the same time from the new target file? Having INDEX&MATCH formula fetch the needed information from another workbook would theoretically work but to my knowledge requires the other workbook to be open at all times which will undoubtedly start to cause unnecessary issues.
My current VBA for work form creating is something like this:
Sub Save_copy()
Dim FileName As String
With ActiveWorkbook
[H3] = Format(Now, "dd.mm.yy_hhmm")
Range("H2").Value = Range("H1").Value
FileName = "SERVICE " & _
Range("H1").Value & _
" - " & Format(Now, "dd.mm.yy") & _
"_" & Format(Now, "hhmm") & _
"." & Right(.Name, Len(.Name) - InStrRev(.Name, "."))
.SaveCopyAs "G:\SERVICE" & "\" & FileName
End With
Call Reset
End Sub
If I understood you properly try something like this ("air-coded" so there may be typos):
Sub Save_copy()
Dim FileName As String
With ActiveWorkbook
[H3] = Format(Now, "dd.mm.yy_hhmm")
Range("H2").Value = Range("H1").Value
FileName = "SERVICE " & _
Range("H1").Value & _
" - " & Format(Now, "dd.mm.yy") & _
"_" & Format(Now, "hhmm") & _
"." & Right(.Name, Len(.Name) - InStrRev(.Name, "."))
.SaveCopyAs "G:\SERVICE\" & FileName
End With
Dim newWorkbook As Excel.Workbook
Set newWorkbook = Workbooks.Open("G:\service\" & FileName)
newWorkbook.Worksheets("A").Delete
newWorkbook.Close True
Reset
End Sub
Additionally, a couple of coding tips:
There's no need for Call - that function is deprecated and only exists to keep ancient code from blowing up
There is an extra concatenation of the "\" in your .SaveCopyAs line - simply put the trailing slash in with the rest of the path (as I did).
The unqualified Range("H2") refers to the ActiveWorksheet and could blow up on you if your user ever happens to click on a different worksheet while your code is running

File is not saving to newly made folder in VBA

I have a macro that created a folder by data within a pathway, and I want a cut of a manager roster to be saved in that folder. Since the folder name varies, this needs to be dynamic.
I want it to go something like this:
Dim sPath As String
sPath = "M:\mgr1_TCR_Reports\"
If Len(Dir(sPath & "_" & Format(Date, "mm_dd_yyyy"), vbDirectory)) = 0 Then
MkDir (sPath & "_" & Format(Date, "mm_dd_yyyy"))
End If
End Sub
and saving this like:
.SaveAs Filename:="M:\mgr1_TCR_Reports\" & "_" & Format(Date, "mm_dd_yyyy_") & "\" & Manager, FileFormat:=xlOpenXMLWorkbook, Password:=""
.Close
But I keep getting a runtime 1004: document not saved on ^^^ the second line of code I provided.
Any idea what's going on?

Excel/VBA Remove text from ThieWorkbook.Name

I am trying to save a copy of an excel file through use of a marco but amend text after the current file name when saving. I have a macro that works, but it adds the file extension to the file name before I can amend text to it.
EG- my file is named "MyCurrentFile.xlsm", when I save it it adds the date, but keeps names the file "MyCurrentFile.xlsm01-14-16.xlsm".
Can I somehow remove the first .xlsm?
Code:
Sub Save_With_Todays_Date()
'
' Save_With_Todays_Date Macro
' Save a copy of the workbook with todays date at the end.
ThisWorkbook.SaveCopyAs _
Filename:=ThisWorkbook.Path & "\" & _
ThisWorkbook.Name & _
Format(Date, "mm-dd-yy") & ".xlsm"
End Sub
You can use the Workbook.FullName property and parse off the extension.
Dim fpfn as String
fpfn = ThisWorkbook.FullName
ThisWorkbook.SaveCopyAs _
Filename:=Left(fpfn, InStrRev(fpfn, Chr(46)) - 1) & Format(Date, "mm-dd-yy"), _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
I would recommend leaving the extension off the Workbook.SaveAs method and let the XlFileFormat Enumeration assign the correct extension. Hardcoding the extension reduces functionality and can result in an incorrect extension being applied to a SaveAs.
ThisWorkbook.Name = Replace(ThisWorkbook.Name, ".xlsm", Format(Date, "mm-dd-yy") & ".xlsm")

Excel VBA When using SaveAs Filename, strange things are happening to the file extension?

This is the line in my code:
ActiveWorkbook.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy" & ".xlsx"), FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Here's the extension from a couple of the files:
.xl47x
.xl35x
I tried using ".xlsm" and I got:
.xl78
.xl22
I can force change the name and then the file will open - but why is it changing the extension? Does someone know what is going on? I've never seen this. Thank you!
Your file extension in the code needs to be outside the Format function. See the suggested approach below.
ActiveWorkbook.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

run-time error 1004 on backup macro that worked before and works on other users

I have a backup macro that runs every time when I save my excel file and saves a copy of the workbook into a folder.
Now I got a new computer where I use the same file, and it does not work anymore, I get run-time error 1004.
My co worker uses the same excel file and the same computer with another user and for him the macro works perfectly as it used to work for me on the other computer.
Code:
'backup
ora = ".h" & Hour(Now)
bufolder = ThisWorkbook.Path & "\excel_backups"
If Len(Dir(bufolder, vbDirectory)) = 0 Then
MkDir bufolder
End If
excfile = ThisWorkbook.Path & "\excel_backups\backup_" & Format(Date, "yyyy/mm/dd") & ora & "_" & ActiveWorkbook.name
If Dir(excfile) = "" Then
ActiveWorkbook.SaveCopyAs Filename:=bufolder & "\backup_" & Format(Date, "yyyy/mm/dd") & ora & "_" & ActiveWorkbook.name
End If
Edit: I get the error on line:
ActiveWorkbook.SaveCopyAs Filename:=bufolder & "\backup_" & Format(Date, "yyyy/mm/dd") & ora & "_" & ActiveWorkbook.name
It says:
Microsoft Office Excel cannot access the file '...'
There are several
possible reasons:
The file name of path does not exit. The file is being used by another
program. The Workbook you are trying to save has the same name as a
I don't think any of these problems may cause the problem.
Thank you for your time
The file cannot be saved because you are attempting to save the file name with your date formatted as "yyyy/mm/dd"? My computer will not allow me to save a file name with backslashes in it. Try changing the Format function to Format(Date, "yyyy-mm-dd").