
[英]Saving Excel sheet as tab-delimited text file without automatic double quote delimiters
[英]Procedure to export a text file with quote delimiters in row 1 - Excel
我已经从Microsoft支持网站LINK下面获得了代码
问题是它工作得太好了。 我只需要带有“”限定符的列的第一行(基本上是表标题)。 下面的代码将“”应用于所有单元格。
我不知道如何进行更改。 任何帮助将不胜感激!
非常感谢,OM
Sub QuoteCommaExport()
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
DestFile = "C:\Users\Documents\Data\test.txt"
FileNum = FreeFile()
On Error Resume Next
Open DestFile For Output As #FileNum
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
On Error GoTo 0
For RowCount = 1 To Selection.Rows.Count
For ColumnCount = 1 To Selection.Columns.Count
Print #FileNum, """" & Selection.Cells(RowCount, _
ColumnCount).Text & """";
If ColumnCount = Selection.Columns.Count Then
Print #FileNum,
Else
Print #FileNum, ",";
End If
Next ColumnCount
Next RowCount
Close #FileNum
End Sub
电子表格中正在选择的数据如下所示:
Date Close Open High Low
24/04/2008 0.9399 0.9472 0.9484 0.9372
25/04/2008 0.9338 0.9394 0.9423 0.9289
28/04/2008 0.9382 0.9339 0.9405 0.9332
我需要的输出如下所示:
"Date","Close","Open","High","Low"
22/06/2015,21,20.698,21.019,20.575
23/06/2015,20.508,20.96,21.052,20.318
24/06/2015,20.679,20.475,20.709,20.287
根据@DisplayName的代码输出
"Date","Open","High","Low","Close","Volume"
7/08/2015 , 3.84145514 , 4.80521243 , 3.4206597 , 3.76001086 , 164329
8/08/2015 , 3.78715895 , 3.800733 , 0.97017103 , 1.02256685 , 674188
9/08/2015 , 0.95851228 , 1.19425818 , 0.85406678 , 0.95275825 , 532170
是否可以删除逗号之间的空格?
尝试这个:
Sub QuoteCommaExport()
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
DestFile = "C:\Users\Documents\Data\test.txt"
FileNum = FreeFile()
On Error Resume Next
Open DestFile For Output As #FileNum
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
Goto CleanExit
End If
On Error GoTo 0
For ColumnCount = 1 To Selection.Columns.Count
Print #FileNum, """" & Selection.Cells(1, _
ColumnCount).Text & """";
If ColumnCount = Selection.Columns.Count Then
Print #FileNum,
Else
Print #FileNum, ",";
End If
Next
For RowCount = 2 To Selection.Rows.Count
For ColumnCount = 1 To Selection.Columns.Count
If ColumnCount = Selection.Columns.Count Then
Print #FileNum, Selection.Cells(RowCount, ColumnCount)
Else
Print #FileNum, Selection.Cells(RowCount, ColumnCount); ",";
End If
Next ColumnCount
Next RowCount
CleanExit:
Close #FileNum
End Sub
一种更简单的方法是使用vbCrLf在开始新行时创建字符串变量,然后在末尾进行单次打印? 我认为这解决了格式问题:
Sub QuoteCommaExport()
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
DestFile = "C:\Users\Documents\Data\test.txt"
FileNum = FreeFile()
On Error Resume Next
Open DestFile For Output As #FileNum
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
Goto CleanExit
End If
On Error GoTo 0
Dim PrintString As String
For ColumnCount = 1 To Selection.Columns.Count
PrintString = PrintString & """" & Selection.Cells(1, ColumnCount).Text & """" & IIf(ColumnCount = Selection.Columns.Count, vbCrLf, ",")
Next
For RowCount = 2 To Selection.Rows.Count
For ColumnCount = 1 To Selection.Columns.Count
PrintString = PrintString & Selection.Cells(RowCount, ColumnCount) & IIf(ColumnCount = Selection.Columns.Count, vbCrLf, ",")
Next ColumnCount
Next RowCount
Print #FileNum, PrintString;
CleanExit:
Close #FileNum
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.