逐行写CSV文件

时间:2014-06-04 22:56:19

标签: excel vba excel-vba csv

我试图在VBA中创建一个CSV输出文件,但我似乎无法得到它。我需要遍历电子表格并从列中提取数字'我'基于列D是否具有" 1"在它与否。有人可以请求语法帮助吗?我想加入以下循环部分。

Dim iRow as Integer

'Loops through worksheet starting at row 2
For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
'Determines whether there's a 1 in column D or not. If there is, copy the file number from row I
If Trim(range("D" & iRow)) <> "" Then
'insert code to pull file numbers from row I and then output it to a CSV file
End If

1 个答案:

答案 0 :(得分:2)

Dim iRow As Integer
Dim stringToWrite As String

For iRow = 2 To ActiveSheet.UsedRange.Rows.Count
    If Cells(iRow, 4) = 1 Then
        If Len(stringToWrite) > 0 Then stringToWrite = stringToWrite & ", "
        stringToWrite = stringToWrite & Cells(iRow, 9)
    End If

Next iRow

Open "c:\Test.csv" For Output As #1
    Print #1, stringToWrite
Close #1