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

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

Related

vba save as pdf into a shared folder with different computer

Hi I wrote a code where it saves the excel sheet as PDF file into a our company's sharefolder (dropbox). I realized when my coworker tried to use that Macro, it doesn't work because of the path the file is saved.
in the code, where it says "MyComputerName" is what my computer name and i am guessing it's because my co workers computer name is different so it can't find the path on her computer.
Is there a way to solve this? so we both can use this macro and save it into the shared folder ?
Help!!!
Sub SaveAsPDF()
' FormatName
ActiveSheet.Name = "#" & ActiveSheet.Range("F6").Value & " " & ActiveSheet.Range("F4").Value
' saveAsPDF Macro
ActiveSheet.ExportAsFixedFormat Type:=xltypepdf, Filename:= _
"C:\Users\MyComputerName\Dropbox\Team Folder\PACKING LIST\201804\" & "PACKING LIST_" & ActiveSheet.Name _
, quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
Try adding the following lines to the beginning of your code:
Dim username As String
username = Environ$("username")
And then your path should be:
"C:\Users\" & username & "\Dropbox\...
To make the year/month dynamic (assuming based on today's date), your link can be:
...LIST\" & Format(Now(), "yyyymm") & "\PACKING LIST...

Naming a workbook with a password

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"

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")

Save as different file type

I would like to make my macro able to save a xlsx file as csv exactly with the same name when running it.
This is what I tried:
ActiveWorkbook.saveas Filename:=ActiveWorkbook.Path & "\" & _
ActiveWorkbook.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
However, it saves the file as .xlsx.csv (i.e, a file called prices.xlsx is saves as prices.xlsx.csv)
How could I save the file with a different file extension, without the .xlsx?
Filename := ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name,".xlsx", ".csv")
If you want to make it more failsafe, in case your extension may be xls, xlsm, xlsb, you can do something like this:
Dim parts As Variant
parts = Split(ActiveWorkbook.Name, ".")
parts(UBound(parts)) = "csv"
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & _
Join(parts, "."), FileFormat:=xlCSV, CreateBackup:=False
It's probably not 100% bulletproof, although I'm struggling to think of a situation where it would not work as expected.

Issue with Saving Active Sheet to New workBook

I am trying to save an Active sheet in Excel using following VBA:
Application.CutCopyMode = False
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
Activeworkbook.close
but I am having two issues here:
1- When I want to open the file I am encountring with following Message:
Manually opening the file is OK by pressing the Yes but I am going to use the Excel File in GIS software which causing problem because of misunderstanding of format. As you can see it has .xls format
2- the Activeworkbook.close is not functioning since I have to close the Application after running the code by my own!
The first part is very important for me, to understand why this is happening? can you please let me know why?
You are using the wrong file format.
For .xls it is xlExcel8. xlOpenXMLWorkbookMacroEnabled is for .xlsm
Either use this
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlExcel8
or use this
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
Regarding your 2nd question. Change your code to this
Application.DisplayAlerts = False
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlExcel8
With ActiveSheet.UsedRange
.Copy
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.Quit