每次使用此代码时,单元格的输出从开始到结束都会出现双引号。我也有尾随选项卡的问题。无论如何,如何删除这些引号和尾部的制表符?
我尝试使用基本的VBA,在该VBA中,它从特定列中复制数据并将其转换为txt文件。
我尝试使用下面的代码,但输出是一个空白的ptxt文件。它甚至没有输出任何东西。我是VBA的新手,所以我们将不胜感激。
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
无拖曳标签或引号
答案 0 :(得分:0)
我认为这就是您要寻找的。不知道您是否希望每个单元格都在文本文件中成为其自己的行,但这就是该单元格的打印方式。这也消除了复制到剪贴板的需要,这通常是不理想的。
这还将自动覆盖现有文件,因此无需提前检查。
Private Sub CommandButton1_Click()
dim filename as string
dim chrstr as string
dim cellvar as variant
dim fso as object
dim txtfso as object
Set fso = CreateObject("Scripting.FileSystemObject") 'Late bound, if you want early binding you will need to add in the reference
Set txtfso = fso.CreateTextFile(ThisWorkbook.Path & "\2019 NERC N1 Contingencies.txt", True)
chrstr = ""
for each cellvar in Range("A2", Cells(Rows.Count, "A").End(xlUp)) 'If you have a ton of rows you will want to load this range into an array and access the array elements over the sheet.
chrstr = chrstr & cellvar.value & vbnewline 'I don't know what your desired output is, mess with this line to change how it looks
next
txtfso.writeline (chrstr)
txtfso.close
end sub