Solved

Problem with file

Posted on 2015-02-04
4
82 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 31

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now