我从微软支持网站LINK
获得了以下代码问题是它运作得有点好。我只需要#34;"列的第一行(基本上是表头)。预选赛。下面的代码应用了""对所有细胞。
我不知道如何做出这种改变。任何帮助将不胜感激!
非常感谢, 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&#39代码的输出
"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
是否可以删除逗号之间的空格?
答案 0 :(得分:0)
试试这个:
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
答案 1 :(得分:0)
更简单的方法是使用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