Every time I use this code, double quotation marks appear for cell's output from beginning to ending. Also I have issue with trailing tabs. Anyway how I can remove these quotation marks and trailing tabs?
I tried using a basic VBA, where it copies data from a certain column and converts it to a txt file.
I tried using the code below but the ouput was a blank ptxt file. It did not even output anything. I am new to VBA so any help would be appreciated.
Private Sub CommandButton1_Click()
Dim s As String, FileName As String, FileNum As Integer
' Define full pathname of TXT file
FileName = ThisWorkbook.Path & "\2019 NERC N1 Contingencies.txt"
' Copy range to the clipboard
Range("A2", Cells(Rows.Count, "A").End(xlUp)).Copy
' Copy column content to the 's' variable via clipboard
With New DataObject
.GetFromClipboard
s = .GetText
End With
Application.CutCopyMode = False
' Write s to TXT file
FileNum = FreeFile
If Len(Dir(FileName)) > 0 Then Kill FileName
Open FileName For Binary Access Write As FileNum
Put FileNum, , s
Close FileNum
'-----------------------Get rid of trailing tabs
Dim sTemp As String
Open ThisWorkbook.Path & "\2019 NERC N1 Contingencies.txt" For Output As #1
For Each r In Range("A2", Cells(Rows.Count, "A").End(xlUp))
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c
'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1
End Sub
Ouput: [Blank Page]
Desired Ouptut:
CON=10 NO TRAILING TAB OR QUOTATION MARK
RangeandCellsare implicitly referring to whatever theActiveSheetis. - Mathieu Guindon