Handling user actions when exporting range as .txt file

edith88beatrix

New Member
Joined
Oct 31, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi!
I have a code to export a selected range from a sheet as .txt file. It works fine. It opens a save as window.
If the user saves the file, a confirmation message appears "File saved successfully".
However, if the user chooses Cancel, the same message appears.
How can I edit the code, so that when the user chooses Cancel, nothing happens (no prompt)? The Excel itself should remain open so the user can continue to work on it.

VBA Code:
Sub SaveToText()
Dim myFile As String, rng As Range, cellValue As String, i As Integer, j As Integer
currentDateAndTime = Now()
myFile = Application.GetSaveAsFilename(fileFilter:="Text (*.txt), *.txt")
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
 For j = 1 To rng.Columns.Count
 cellValue = rng.Cells(i, j).Value
 If j = rng.Columns.Count Then
 Print #1, cellValue
 'Write #1, If you want the data as comma separator
 Else
 Print #1, cellValue,
 'Write #1, If you want the data as comma separator
 End If
 Next j
Next i
Close #1
MsgBox ("File saved successfully.")
End Sub

Thank you!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I left your code largely intact. There is an added check to see if the Cancel button was selected...myFile = "False" if the Cancel button is selected.
VBA Code:
Sub SaveToText()

    Dim myFile As String, rng As Range, cellValue As String, i As Integer, j As Integer

    myFile = Application.GetSaveAsFilename(fileFilter:="Text (*.txt), *.txt")

    If myFile <> "False" Then
        Set rng = Selection

        Open myFile For Output As #1

        For i = 1 To rng.Rows.Count
            For j = 1 To rng.Columns.Count
                cellValue = rng.Cells(i, j).Value
                If j = rng.Columns.Count Then
                    Print #1, cellValue
                    'Write #1, If you want the data as comma separator
                Else
                    Print #1, cellValue,
                    'Write #1, If you want the data as comma separator
                End If
            Next j
        Next i

        Close #1

        MsgBox ("File saved successfully.")
    End If

End Sub
I hope this helps,

Doug
 
Upvote 0
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top