+ Reply to Thread
Results 1 to 6 of 6

File saving error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2014
    Location
    Hyderabad
    MS-Off Ver
    MS Office 2021
    Posts
    347

    File saving error

    Sub Saveecsval()
    Dim ans As Long
    Dim lastRow As Long
    Dim sSaveAsFilePath As String
    Dim FileName As Variant
    Dim Sep As String
        
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        ActiveSheet.PageSetup.PrintArea = Selection.Address
    
        On Error GoTo ErrHandler:
        
        FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="Text Files (*.txt),*.txt")
        If FileName = False Then
            ''''''''''''''''''''''''''
            ' user cancelled, get out
            ''''''''''''''''''''''''''
            Exit Sub
        End If
        Debug.Print "FileName: " & FileName, "Separator: " & Sep
        ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), _
           SelectionOnly:=False, AppendData:=True
    
    
            If Dir(sSaveAsFilePath) <> "" Then
            ans = MsgBox("File " & sSaveAsFilePath & " exists.  Overwrite?", vbYesNo + vbExclamation)
            If ans <> vbYes Then
                Exit Sub
            Else
                Kill sSaveAsFilePath
            End If
        End If
        Sheet7.Copy '//Copy sheet 7 to new workbook
        ActiveWorkbook.SaveAs sSaveAsFilePath, xlTextPrinter '//Save as Formatted Text (Space Delimited) file
        If ActiveWorkbook.Name <> ThisWorkbook.Name Then '//Double sure we don't close this workbook
            ActiveWorkbook.Close False
        End If
    
    My_Exit:
    Exit Sub
    
    ErrHandler:
    MsgBox Err.Description
    Resume My_Exit
    End Sub
    In the code when I run it, everything is working fine except when I give file name after prompting where to save, it is prompting "File exists. Overwrite?", though no file exists with such name and also, if I say yes, it says "File Not Found". Silver lining is though error is occuring, file is being saved. Help needed to get rid of the error prompting.

    I have to submit that I google and found the bits and pieces of the code which I clubbed to get this code.
    Attached Files Attached Files
    Last edited by Ravana; 03-10-2015 at 09:13 AM.

  2. #2
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: File saving error

    Can u share the excel file... as ExportToTextFile gives error.. missing function code...
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  3. #3
    Forum Contributor
    Join Date
    09-16-2014
    Location
    Hyderabad
    MS-Off Ver
    MS Office 2021
    Posts
    347

    Re: File saving error

    I have attached the excel file.

    I use the code assign to button "SAVE AS TXT FILE" ON THE SHEET "ECS VAL"

  4. #4
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: File saving error

    Hi Ravana,

    Please find the attached file...Run & Check for your conditions
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    09-16-2014
    Location
    Hyderabad
    MS-Off Ver
    MS Office 2021
    Posts
    347

    Re: File saving error

    @pARTH007....Me Happy...Glad u helped me...Thanks a lot.

    What if a file already exists with the name I am giving. Shouldn't it warn me to "Over write?", If yes the proceed other-wise choose another name.

  6. #6
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: File saving error

    Trying to fix this.. but not getting correctly..
    Experts please help
    Last edited by Parth007; 03-10-2015 at 09:57 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Error in opening file and saving a file
    By uday1969 in forum Excel General
    Replies: 1
    Last Post: 11-03-2012, 06:21 PM
  2. Error handling - Saving file
    By siray13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2012, 10:58 AM
  3. Error when saving to file
    By soreno in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2010, 05:54 PM
  4. error saving excel file
    By tek9step in forum Excel General
    Replies: 3
    Last Post: 08-12-2009, 04:43 AM
  5. Error In Saving Excel File
    By Nikhil Tambe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2005, 02:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1