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.
Bookmarks