export excel data to text file and save it using cell value as the file name

matrix26

Board Regular
Joined
Dec 16, 2020
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code that allows me to select specific cells in an excel document and export them to a text file.
I want to modify the code so it will pre-fill the filename with the value found in cell B2.
I've found many examples of how to do this but just can't get any of the examples to work within my code.
Can anyone help out?
Thank you in advance


Sub ExportRangetoFile()
'Update 20210310

Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "COPY COLUMN B"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
wb.SaveAs FileName:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
VBA Code:
Option Explicit

Sub ExportRangetoFile()
'Update 20210310

Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
xTitleId = "COPY COLUMN B"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Range("B2").Value
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Sub ExportRangetoFile() 'Update 20210310 Dim wb As Workbook Dim saveFile As String Dim WorkRng As Range On Error Resume Next xTitleId = "COPY COLUMN B" Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8) Application.ScreenUpdating = False Application.DisplayAlerts = False Set wb = Application.Workbooks.Add WorkRng.Copy wb.Worksheets(1).Paste saveFile = Range("B2").Value wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False wb.Close Application.CutCopyMode = False Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub
Hi,

Thanks, but this and doesn't work.
 
Upvote 0
Made two changes to the macro. This works here :

VBA Code:
Option Explicit

Sub ExportRangetoFile()
'Update 20210310

Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
'xTitleId = "COPY COLUMN B"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Range("B2").Value
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Made two changes to the macro. This works here :

VBA Code:
Option Explicit

Sub ExportRangetoFile()
'Update 20210310

Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
'xTitleId = "COPY COLUMN B"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Application.Workbooks.Add
WorkRng.Copy
wb.Worksheets(1).Paste
saveFile = Range("B2").Value
wb.SaveAs Filename:=saveFile, FileFormat:=xlText, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
I appreciate your help, this doesn't work either.
The script runs up to the selection of cells then ends without saving anything.

Thank you for trying.
 
Upvote 0
I don't understand. The code works here.
 
Upvote 0
I don't understand. The code works here.
I'm wondering if it's because I'm using a work laptop and I need to also set a path string.
Let me try that and I'll get back to you.
 
Upvote 0
Got it to work.
I added this line
InitialFileName = Range("B2")

And changed this line
saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
to
saveFile = Application.GetSaveAsFilename(InitialFileName, fileFilter:="Text Files (*.txt), *.txt")

Thank you for all of your help.
I really appreciate it
 
Upvote 0
Solution

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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