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
Related
I have a simple code, which creates saves the actual file as csv file in another folder. How can I open this recently created file in notepad after the csv was created?
Here is the code, I tried with Call Shell but it didn't work.
Sub ConvertTocsv()
ChDir "S:\Back Office\Tradar\DailyReportBDP"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
("S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"), FileFormat:=xlCSV, CreateBackup:=True, Local:=True
Application.DisplayAlerts = True
Information.Show
Call Shell("explorer.exe" & " " & "S:\Back Office\Tradar\DailyReportBDP", vbNormalFocus)
End Sub
Please provide some input. Thank you.
Solved it, here:
Sub ConvertTocsv()
Dim strfilename As String
strfilename = "S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"
ChDir "S:\Back Office\Tradar\DailyReportBDP"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
("S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"), FileFormat:=xlCSV, CreateBackup:=True, Local:=True
Application.DisplayAlerts = True
Information.Show
returnvalue = Shell("notepad.exe " & strfilename, vbNormalFocus)
ActiveWorkbook.Close SaveChanges:=False
End Sub
I have an Excel that gets updated with sales numbers daily. At the end of the week, when the Excel is complete, I export a PDF copy of the WEEKLY worksheet. Once I have a PDF copy, the sales numbers are transferred to another sheet within the workbook, emptying the WEEKLY worksheet.
In addition to this PDF copy of the WEEKLY worksheet, I'd like to export the entire workbook in a separate Excel file to the same location (.xls format is fine). I'd like to do this before emptying the WEEKLY worksheet. I've tried using a save as macro, but I want to remain in my original Excel - not the newly saved file.
For reference, here's the VBA code for my PDF export:
Sub SaveWeekly()
'
' SaveWeekly Macro
'
'
Sheets("WEEKLY").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Z:\Excel New\Previous Excels\" & Range("A1") & " " & Range("H1") & ", " & Format(Date, "yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any help is appreciated.
Try this code
Sub Test()
Dim strDate As String
Dim strTime As String
strDate = Format(Date, "DD-MM-YYYY")
strTime = Format(Time, "hh.mm.ss")
Application.DisplayAlerts = False
With ActiveWorkbook
.SaveCopyAs fileName:=ThisWorkbook.Path & "\" & strDate & "_" & strTime & "_" & .Name
End With
Application.DisplayAlerts = True
End Sub
I'm experienced VBA developer but have no enough experience in Mac to complete my very simple project. I want to export sheets to CSV and save them into the same directory where workbook stored.
I already created a new workbook (ActiveWorkbook) with data copied from the main workbook and fully worked Windows part of code. But in Mac I cant make this part to work, no files created in result:
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
I read few sources and found that Excel for Mac 2016 requires to call GetSaveAsFilename dialog with initial filename with manual confirmation to do it, so I modified my code like this:
#If Mac Then
s_fname = Application.GetSaveAsFilename(InitialFileName:= _
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv")
If s_fname <> False Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs s_fname, 6
Application.DisplayAlerts = True
End If
#Else
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
#End If
Macro creates files with data but they saved in XLSX not CSV:
and dialog have xlsx as default format:
When I'm trying to set FileFilter I have no files saved again (I assume Mac not supports this attribute). When I try to do replacement:
s_fname=replace(s_fname,".xslx",".csv")
I still have .xlsx files in the folder.
Can you suggest please which GetSaveAsFilename FileFilter value I must to set in the code to force CSV save instead of XLSX? Or anotehr VBA solution to do it (file format change, after-save renaming etc).
Thank you,
Viacheslav
Hi try to change first part as this one:
If Mac Then
OutputFile = ThisWorkbook.Path & Replace(ActiveWorkbook.Name, ".xls", "-") & ".csv"
If OutputFile <> False Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=OutputFile, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
End If
I am placing a button on an Excel sheet which would save as the current worksheet to a separate file. Following is the code I am using:
Sub SaveWorkbook()
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\username\Downloads\New folder" & ActiveSheet.Name & ".xlsx", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
Here instead of sheet name (ActiveSheet.Name), I want to place the text from the cell (D2:E2, both the cells are merged).
Is it possible to do?
Also here I have explicitly mentioned the path.
Is it possible to make it prompt to choose the path to save it?
While choosing the path, the name can be taken from the cell?
Update:
Sub SelectFolder()
Dim diaFolder As FileDialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:= _
diaFolder & ActiveSheet.[d2] & ".xlsx", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Set diaFolder = Nothing
End Sub
Can you see what is the mistake here? the file is not saving in the selected path.
Replace ActiveSheet.Name with ActiveSheet.[d2] .
That will pull the value from the merged cell.
As far as getting the path from the user, it easy with:
Application.FileDialog(msoFileDialogFolderPicker)
https://stackoverflow.com/a/5975453/3566998
-Summary:
I'm trying write code that will automatically save with the name of the current date
-Problem: Error saying "Method 'SaveAs' of object '_Workbook' failed" pops up when compiler reaches the line that saves. Everything else works. I've shown the whole function for references' sake.
Function createRecord()
Dim rowCount As Integer
Dim theDate As Date
theDate = Format(Now(), "MM-DD-YY")
Sheets("New Data").Select
Cells.Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Application.ActiveSheet.Name = "ChaseHistory"
ActiveSheet.Paste
rowCount = ActiveSheet.UsedRange.Rows.Count
Sheets("Exceptions").Select
'rowCount = ActiveSheet.UsedRange.Rows.Count
Application.CutCopyMode = False
ActiveSheet.UsedRange.Rows.Select
Selection.Copy
Sheets("ChaseHistory").Select
ActiveSheet.Range("A" & rowCount + 2).Select
ActiveSheet.Paste
Range("A1").Select
Cells.Select
Selection.Copy
ChDir "Z:\Customer_Service_Accounting\REPORTING & CONTROLS TEAM\Book And Balance_Katie\Chase Booking History" 'loads the crystal report
Workbooks.Open Filename:= _
"Z:\Customer_Service_Accounting\REPORTING & CONTROLS TEAM\Book And Balance_Katie\Chase Booking History\Do_Not_Delete.xlsx"
Windows("Do_Not_Delete").Activate
ActiveSheet.Paste
Application.DisplayAlerts = False
'---------------This is the problem child-------------- 'SAVING WORKBOOK
ActiveWorkbook.SaveAs Filename:="Z:\Customer_Service_Accounting\REPORTING & CONTROLS TEAM\Book And Balance_Katie\Chase Booking History\" & CStr(theDate), FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
Application.DisplayAlerts = True
End Function
-I added in the convert to string method on date because I thought that might be causing the problem but had the same result. Let me know if you see anything wrong here. Thanks!
The Problem: because in my code I was disabling prompts from excel, when I was trying to save I wasn't seeing a prompt telling me that I was attempting to save with an improper format.
Basically to sum it up, Excel didn't like that I had backslashes ("/") in my filename (which I really should have known)
The Fix: I ended up using this statement:
ActiveWorkbook.SaveAs Filename:="Z:...\" & "Chase " & _
Month(theDate) & "_" & Day(theDate) & "_" & Year(theDate) & ".xlsx"
So all I really did here was post month, day, and year together into a string separated by underscores to avoid the evil backslash.
Thanks for your help Gaffi!
Have you tried something like this?
ActiveWorkbook.SaveAs Filename:="Z:\Customer_Service_Accounting\REPORTING & CONTROLS TEAM\Book And Balance_Katie\Chase Booking History\" & Format(theDate, "mm.dd.yy"), FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
To highlight: I changed CStr(theDate) to Format(theDate, "mm.dd.yy") & ".xlsx", but you can use other formats if needed.
Explanation:
theDate is of type Date (see: Dim theDate As Date), so what is returned is a complete date/time format string when you use CStr(). This will result in something like this:
Debug.Print CStr(Now())
7/6/2012 7:23:38 AM
Which will likely cause your system to reject for invalid characters in the filename.