Hi all,
I'm a little stuck. I have a spreadsheet which I am going to use to generate some values which then need to be appended to the first blank row in an existing .csv file.
This is what I have so far:
1st problem
Whilst the data is within the range A3:Q22 it could be any number of rows long between 1 & 20.
So my script is currently adding blank rows in as it copies the entire range regardless of whether it has data in or not.
2nd problem
The first line of the import is going wrong. It is only importing cell A3. Cell B3:Q3 then imports on the second line. When I get to the next line it works correctly (importing cells A4:Q4).
Any help, as always, would be appreciated.
I'm a little stuck. I have a spreadsheet which I am going to use to generate some values which then need to be appended to the first blank row in an existing .csv file.
This is what I have so far:
Code:
Sub Append2CSV()
Dim tmpCSV As String 'string to hold the CSV info
Dim f As Integer
Const CSVFile As String = "Y:\Robot\Jobs.csv"
f = FreeFile
Open CSVFile For Append As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=f]#f[/URL]
tmpCSV = Range2CSV(Range("A3:Q22"))
Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=f]#f[/URL] , tmpCSV
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=f]#f[/URL]
ActiveWorkbook.FollowHyperlink Address:=CSVFile
End Sub
Function Range2CSV(list) As String
Dim tmp As String
Dim cr As Long
Dim r As Range
If TypeName(list) = "Range" Then
cr = 1
For Each r In list.Cells
If r.Row = cr Then
If tmp = vbNullString Then
tmp = r.Value
Else
tmp = tmp & "," & r.Value
End If
Else
cr = cr + 1
If tmp = vbNullString Then
tmp = r.Value
Else
tmp = tmp & Chr(10) & r.Value
End If
End If
Next
End If
Range2CSV = tmp
End Function
1st problem
Whilst the data is within the range A3:Q22 it could be any number of rows long between 1 & 20.
So my script is currently adding blank rows in as it copies the entire range regardless of whether it has data in or not.
2nd problem
The first line of the import is going wrong. It is only importing cell A3. Cell B3:Q3 then imports on the second line. When I get to the next line it works correctly (importing cells A4:Q4).
Any help, as always, would be appreciated.