1

I'm trying to create a CSV output file in VBA but I can't seem to get it. I need to loop through a spreadsheet and pull numbers from column 'I' based on whether column D has a "1" in it or not. Could someone please help with the syntax? I'd like to incorporate the following loop portion.

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
RubberDuck
  • 11,933
  • 4
  • 50
  • 95
Chicken_Hawk
  • 71
  • 1
  • 10

1 Answers1

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
MatthewHagemann
  • 1,167
  • 2
  • 9
  • 20