I am almost finished with a macro to export data from excel to a .txt file. However, when I run the macro, the resulting file has trailing spaces. (See incorrect .txt file) I can't have this, since, where I am uploading will not accept the data. See correct .txt file for what I need-nothing too crazy, just no extra spaces after exported data. Macro is:
Option Explicit
Public Sub ExportToNotepad()
Dim ws As Worksheet
Dim wsData As Variant
Dim myFileName As String
Dim FN As Integer
Dim p As Integer, q As Integer
Dim path As String
Dim myString As String
Dim lastrow As Long, lastcolumn As Long
Set ws = Worksheets("Sheet1")
lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
lastcolumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
path = Application.ThisWorkbook.path & "\"
'"C:\Users\wbamford\ShareFile\Shared Folders\NO-Accounting P-Drive\2018"
For p = 1 To lastcolumn
wsData = ws.Cells(1, p).Value
If wsData = "" Then Exit Sub
myFileName = wsData
myFileName = myFileName & ".txt"
myFileName = path & myFileName
MsgBox myFileName
For q = 2 To lastrow
myString = myString & "" & ws.Cells(q, p) & vbCrLf
FN = FreeFile
Open myFileName For Output As #FN
Print #FN, myString
Close #FN
Next q
myString = ""
Next p
End Sub
Bookmarks