Solved

Problem with file

Posted on 2015-02-04
4
83 Views
Last Modified: 2015-02-04
Hi,
when running these

Sub CreateNewFile()
    Dim objShtSrc As Excel.Worksheet
    Dim objShtDst As Excel.Worksheet
    
    Dim intCurRow As Integer
    
    VActiveWorkbookName = ActiveWorkbook.Name
    
    If VActiveWorkbookName <> "Test.xlsm" Then
        Exit Sub
    End If


'--------------
' Retrieve data
'--------------
    Worksheets("Details").Select
    Range("E5").Select
    Selection.QueryTable.Refresh BackgroundQuery:=False

    Worksheets("Report").Select

    myPath = ActiveWorkbook.Path
    
    sFileName = myPath + "\Test " + Worksheets("Details").Range("B1").Value + ".xlsm"
    Set objFSO = Nothing
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(sFileName) Then
        objFSO.DeleteFile (sFileName)
    End If
    ActiveWorkbook.SaveAs Filename:= _
        sFileName _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    MsgBox ("File saved!")
      
    Application.SendKeys "{ESC}"

End Sub

Open in new window


to create one other .xlsm file, using Excel 2010. The newly created file cannot be opened, as it says "... file format or file extension is not valid ..". what to adjust to the codes?
0
Comment
Question by:HuaMinChen
4 Comments
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 40588313
replace this line
    ActiveWorkbook.SaveAs  Filename:= _
        sFileName _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False


By this line
    ActiveWorkbook.SaveAs  Filename:= _
        sFileName _
        ,  Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False

gowflow
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40588466
If that doesn't work, can you run & break the code on this line :

Set objFSO = Nothing

Open in new window


..and report back the value of sFileName
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40588487
Replace xlNormal with xlOpenXMLWorkbookMacroEnabled
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 40588534
Might not be the case but line 25 of the code where you are creating the variable "sFileName" you have plus signs which in theory is adding the various items together; as they are text that will cause an error.

Try changing the plus signs to &:

sFileName = myPath & "\Test " & Worksheets("Details").Range("B1").Value & ".xlsm"

Thanks
Rob H
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question