如何从VBA创建单独的CSV文件? [英] How to create a separate CSV file from VBA?
本文介绍了如何从VBA创建单独的CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要输出一些结果作为 .csv
文件,稍后由另一个进程解析。为了产生这些结果,我有一个巨大的工作簿,包含所有我需要的宏和函数。
I need to output some results as a .csv
file, that gets parsed later on by another process. In order to produce these results, I have a huge workbook containing all the macros and functions that I need.
- 可以从<$创建一个单独的
.csv
c $ c> VBA ? - 是否可以使用
VBA
功能写入,而不仅仅是以原始文本方式编写?
- Is it possible to "create" a separate
.csv
file fromVBA
? - Is it possible to use
VBA
features to write into it instead of just writing in a "raw textual" approach ?
谢谢:)
推荐答案
Option Explicit
Sub WriteFile()
Dim ColNum As Integer
Dim Line As String
Dim LineValues() As Variant
Dim OutputFileNum As Integer
Dim PathName As String
Dim RowNum As Integer
Dim SheetValues() As Variant
PathName = Application.ActiveWorkbook.Path
OutputFileNum = FreeFile
Open PathName & "\Test.csv" For Output Lock Write As #OutputFileNum
Print #OutputFileNum, "Field1" & "," & "Field2"
SheetValues = Sheets("Sheet1").Range("A1:H9").Value
ReDim LineValues(1 To 8)
For RowNum = 1 To 9
For ColNum = 1 To 8
LineValues(ColNum) = SheetValues(RowNum, ColNum)
Next
Line = Join(LineValues, ",")
Print #OutputFileNum, Line
Next
Close OutputFileNum
End Sub
不要忘记,您需要在包含逗号的任何字段中加上引号。
Don't forget you will need to put quotes around any field containing a comma.
这篇关于如何从VBA创建单独的CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文