将选定单元格的导出写入不带引号的.txt文件 [英] Write export of selected cells as a .txt file without "quotation marks"
本文介绍了将选定单元格的导出写入不带引号的.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一个获得大量信息的Excel工作表。 有些列包含我需要在脚本中使用的信息,并且在单击按钮后,我使用我找到的以下代码来保存我在.txt文件中选择的任何内容。
Private Sub CommandButton21_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "NumeroChamados.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
我保存的所有内容都以引号结尾,如下例所示:
"4146546546523133"
"4285725763131"
"461"
"4230236646435356694197285187451644148"
"4230375763756379653464564"
我选择的单元格通常包含来自另一个单元格的字符串,我使用宏在其中获取该字符串。
推荐答案
若要避免向任何EXCEL解释为字符串的内容(即使是看起来像数字的文本)添加换行引号,请使用Print
而不是Write
。
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "NumeroChamados.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
这篇关于将选定单元格的导出写入不带引号的.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文