I am my wits' end about this issue. The following code steps through the Output Sheet and writes 24 columns of data into 24 separate files. The file names are in a 3x8 grid on the Temp sheet. The data is being output into .json files for upload into ArcGIS. The code works beautifully for me with one exception. If I try to import the files as written, ArcGIS says there's an unexpected character in the file. I don't see any extra characters when I open the file in Notepad, the output looks fine to me. I've tried everything I can think of to delete hidden characters and the only thing that works is: open a new.txt file, copy all the data from the target file, close the target file, and save the new.txt file as the target .json file, overwriting the old file.
Here's my code:
Sub WriteData()
Dim filepath As String
Dim celldata As String
Dim lastrow As String
Dim lastcol As String
Dim k As Long
Dim temp As Worksheet
Dim ouput As Worksheet
Dim fileout As Object
Dim fso As Object
'
Set temp = ActiveWorkbook.Sheets("Temp")
Set output = ActiveWorkbook.Sheets("Output Sheet")
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 15 To 22
For j = 3 To 5
k = 3 * (i - 15) + j - 2
filepath = Application.ActiveWorkbook.Path & "\JSON Files" & temp.Cells(i, 1) & " " & temp.Cells(15, j) & ".json"
Set fileout = fso.CreateTextFile(filepath, True, True)
For l = 1 To Cells(Rows.Count, k).End(xlUp).Row + 1
celldata = output.Cells(l, k)
fileout.write celldata
Next l
fileout.Close
Next j
Next i
Sheets("Output Sheet").Select
ActiveWorkbook.Save
End Sub
Here's my code:
Sub WriteData()
Dim filepath As String
Dim celldata As String
Dim lastrow As String
Dim lastcol As String
Dim k As Long
Dim temp As Worksheet
Dim ouput As Worksheet
Dim fileout As Object
Dim fso As Object
'
Set temp = ActiveWorkbook.Sheets("Temp")
Set output = ActiveWorkbook.Sheets("Output Sheet")
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 15 To 22
For j = 3 To 5
k = 3 * (i - 15) + j - 2
filepath = Application.ActiveWorkbook.Path & "\JSON Files" & temp.Cells(i, 1) & " " & temp.Cells(15, j) & ".json"
Set fileout = fso.CreateTextFile(filepath, True, True)
For l = 1 To Cells(Rows.Count, k).End(xlUp).Row + 1
celldata = output.Cells(l, k)
fileout.write celldata
Next l
fileout.Close
Next j
Next i
Sheets("Output Sheet").Select
ActiveWorkbook.Save
End Sub