逐行写入CSV文件

我试图在VBA中创build一个CSV输出文件,但我似乎无法得到它。 我需要通过电子表格进行循环,并根据列D中是否有“1”来从列“I”中提取数字。 有人可以请帮忙的语法? 我想join下面的循环部分。

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 

 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