Exporting to Text File not working

Mstg007

Active Member
Joined
Dec 30, 2013
Messages
383
I am trying to use the below macro code to export a column within a sheet to a txt file. The final output shows " #REF! " within the text file. Not sure what I did.

VBA Code:
Sub EXPORT()


'Update 20200131
Dim wb As Workbook
Dim saveFile As String
Dim WorkRng As Range
On Error Resume Next
'Set WorkRng = Sheets("Sheet1").Range("B1:B500")
Set WorkRng = Sheets("Sheet1").Range("B1:B" & Cells(Rows.Count, "J").End(xlUp).Row)
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:=xlTextPrinter, CreateBackup:=False
wb.Close
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True


End Sub

Thanks for any help!
 

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.
Works Here.
Book1
ABCD
11
22
33
44
55
6
7
8
Sheet1


Notepad.jpg


Try changing this line
VBA Code:
Set WorkRng = Sheets("Sheet1").Range("B1:B" & Cells(Rows.Count, "J").End(xlUp).Row)

to this
VBA Code:
Set WorkRng = Sheets("Sheet1").Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
 
Upvote 0
Are there errors in the data file you are exporting from?
It might be helpful if you can provide a sample of the file you are trying to export.
 
Upvote 0
Thank you for checking this out. Here is the workbook. I still have no idea.

 

Attachments

  • Capture.JPG
    Capture.JPG
    20.4 KB · Views: 13
Upvote 0
Change
VBA Code:
wb.Worksheets(1).Paste
to
VBA Code:
wb.Worksheets(1).Range("A1").PasteSpecial xlPasteValues
This will paste the values only, rather than the formula.
 
Upvote 0
I think you got it! Thank you so much.

VBA Code:
wb.Worksheets(1).Range("A1").PasteSpecial xlPasteValues

I did not think that would be a difference.
 
Upvote 0
Because your formula was looking at 1 cell to the left, when you paste it to col A there is no 1 cell to the left hence you get the #REF error.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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