2

I'm trying to save out a file using a combination of hard line and cell value to determine the file path.

In cell A29, I have a formula that outputs this:

2014\January\High Cash 1.7.14

I'm getting an Expected: end of statement error.

The code is:

ActiveWorkbook.SaveAs Filename:="S:\IRD\Tamarac\Daily High Cash Reporting\& Range("A29").Text & ".xlsx", FileFormat:= _  xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _ , CreateBackup:=False
1
  • Syntax highlighting of SO already shows the mistake. Commented Jan 7, 2014 at 18:30

2 Answers 2

5

Suggest you go a step further and ensure any invalid file name characters that will case a save error are filtered out

This code removes

[]/:*?"<>

main code

Sub CleanSave()
Dim fileName As String
fileName = "C:\temp\" & strClean(Range("A29").Value) & ".xlsx"
ActiveWorkbook.SaveAs fileName:=fileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

cleaning function

Function strClean(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "[\[\]|\/\\:\*\?""<>]"
    .Global = True
    strClean = .Replace(strIn, vbNullString)
End With
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

I use a different cleaning function but this goes into my database as well :)
Brett and @Siddharth What's the difference between declaring it as a sub and a function?
4

I would re-write as follows:

Dim fileName As String
fileName = "S:\IRD\Tamarac\Daily High Cash Reporting\" & Range("A29").Text & ".xlsx"
ActiveWorkbook.SaveAs Filename:=fileName, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

I think all you are missing is the closing double-quotes after the 'Reporting\' part of your code...

3 Comments

Actually, I'm copying your code now and its giving me an error on the "_" after FileFormat... help?
Try Removing the "_ " before the xlNormal and before CreateBackup. I've edited my suggested answer.
This code created an unreadable workbook for me using Excel 2007. I found the problem was with "FileFormat:=xlNormal". After changing this to "FileFormat:=xlOpenXMLWorkbook", the code now works correctly and creates an excel file that Excel can read. Not sure if this is due to the version of Excel im using etc, but hope this helps anyone that stumbles here with the same problem that I had.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.