在第 1 行中使用引号定界符导出文本文件的过程 - Excel
Procedure to export a text file with quote delimiters in row 1 - Excel
我从 Microsoft 支持网站获得了以下代码 LINK
问题是它运行得有点太好了。我只需要列的第一行(基本上是 table headers)和“”限定符。下面的代码将“”应用于所有单元格。
我不知道如何进行此更改。任何帮助将不胜感激!
非常感谢,
哦
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
我从 Microsoft 支持网站获得了以下代码 LINK
问题是它运行得有点太好了。我只需要列的第一行(基本上是 table headers)和“”限定符。下面的代码将“”应用于所有单元格。
我不知道如何进行此更改。任何帮助将不胜感激!
非常感谢, 哦
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