I've got an issue with my code. It's supposed to run in a file I download each month and export a subset of that data to a csv I can use. This is my code.
Sub Eksporter()
Dim whatever As String
file = FreeFile
filepath = ""
Filename = filepath & "X" & ".csv"
Open Filename For Output As #file
For row = 2 To 10000
whatever = Cells(row, 3) & ";;" & Cells(row, 12) & vbNewLine
Next row
Print #file, whatever
Close #file
End Sub
It runs alright, but only does row 2 before it stops. Any ideas? Any other ways of doing this? I'm an amateur to all of this so any tips appreciated.
CodePudding user response:
Another way, fill an array and use Join to create the string.
Option Explicit
Sub Eksporter()
Const filepath = ""
Dim ws As Worksheet, ff, filename as String
Dim LastRow As Long, r As Long, ar() As String
' read into array
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
LastRow = .Cells(Rows.Count, "C").End(xlUp).row
ReDim ar(0 To LastRow - 2) ' size the array
For r = 2 To LastRow
ar(r - 2) = .Cells(r, "C") & ";;" & .Cells(r, "L")
Next
End With
' output array to file
filename = "X_" & Format(Now, "yyyymmdd_HHMMSS") & ".csv"
ff = FreeFile
Open filepath & filename For Output As #ff
Print #ff, Join(ar, vbNewLine)
Close #ff
MsgBox LastRow - 1 & " rows exported to " & filepath & filename
End Sub