I am creating a macro of the report generator that let the user to save a copy of the file to its destination.
Cell value (“E5”) is where the user input the date.
Cell value (“E11”) is where user keyin the record name (in this case colour values)
The macro will save it to the location in the C drive
Here are the code :
Sub CTemplate() 'Select up the macro generator Sheets("File Generator").Select 'Save file according to the textbox values Dim filename As String Dim varDatevalue As String Dim varColourvalue As String varDatevalue = Range("E5").Value varColourvalue = Range("E11").Value ActiveWorkbook.SaveAs filename:="C:\Colour Log\" & varDatevalue & "--" & varColourvalue & ".xlsm", _ FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
I already tried as followed:
- Debugging and search for SO but couldn’t find any one else with the same problems
- I already created the folder at the desired locations
- Uncheck (“Read Only”) check box for the file so it can be written
Thank you .
“A filename cannot contain any of the following characters: \ / : * ? ” < > |” – your file name seems to be “5\11\4192C700” which effectively means that you are trying to save your file in a non-existent directory c:\Colour Log\5\11\4192C700. You have to change the slashes in the file name for other characters.
The ‘\ / : * ? < > | [ ] “‘ Issue
Sub CTemplate() 'Always place values, especially text into constants, so you can 'quickly change them and you don't have to search and change them 'wherever they appear in the code. Const cStrPath As String = "C:\Colour Log\" Const cStrWsName As String = "File Generator" Const cStrDateCell As String = "E5" Const cStrColorCell As String = "E11" Dim arrNope As Variant Dim strNope As String Dim strFileName As String Dim strDate As String Dim strColour As String Dim intNope As Integer 'Characters you can't have in a filename strNope = "\ / : * ? < > | [ ] " & Chr(34) 'Chr(34) is double quotes (") 'You can add other characters like "." if you don't want them in the 'filename, just make sure to separate the characters and end the string 'with a space (" "). 'Paste the characters into an array arrNope = Split(strNope) 'Calculate strings With Worksheets(cStrWsName) 'Loop through the array of characters For intNope = LBound(arrNope) To UBound(arrNope) 'With 'Cstr' you coerce each value to a string data type. 'With 'Replace' you replace each character with "", practically you 'delete each 'unwanted' character if it is found. strDate = Replace(CStr(.Range(cStrDateCell).Value), _ arrNope(intNope), "") Next 'Coerce the value to a string datatype strColour = CStr(.Range(cStrColorCell).Value) End With 'Calculate filename strFileName = cStrPath & strDate & "--" & strColour & ".xlsm" 'The following line is used only to suppress the error that could occur when 'a file already exists and at the prompt "No" or "Cancel" is selected. On Error Resume Next 'Save the file ActiveWorkbook.SaveAs filename:=strFileName, _ FileFormat:=xlOpenXMLWorkbookMacroEnabled End Sub