I have written txt-files with excel and a macro in the past several times. I didn't hit 10000 lines or more. Never say Never...
My .csv file has over 87000 rows like that example "15k50,CityABC,56ab,CountryofCity,ID,Street". I use the Split() function in order to separate the values. The Macro formated and wrote the values as single lines to the txt file.
Around 9800 lines the txt-file closed... But why? I tried with Slepp() to make sure the print algho isn't overloaded or something else.
The counter 10000 is there because I want to make it easer to understand for you. If it goes over 10000 the problem is "solved".
Information txt-File format:
- ASCII
- Unix (LF)
Shortcut, after several comments
- with Minimal, Reproducible Example overworked the code (deleted sleep, simplification variable names, try to make code from scratch)
- changed
SplitString()
toSplit()
, because call over function is stupid... - after printed line 9000 to the txt-file the following error popup "Run-Time Error 5: Invalid Procedure Call or Argument" at code line
fso.WriteLine ("# " & strArr(0) & " # " & strArr(1) & ...
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If
Sub formattedToTxt()
Dim strArr() As String
Dim strB As String
Dim intC As Integer
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
strB = filePathExport & "\" & filenameExport & fileFormatExport
Set fso = fso.CreateTextFile(strB, True)
Do While counter <= 10000
strArr = Split(ActiveCell.Value, ",")
intC = CalcWhitespace(strArr(5), 40)
fso.WriteLine ("# " & strArr(0) & " # " & strArr(1) & " # " & strArr(2) & " # " & strArr(3) & " # " & strArr(4) & " # " & strArr(5) & Space(intC) & "#")
ActiveCell.Offset(1, 0).Select
If ((counter Mod 1000) = 0) Then
Debug.Print ("Entry " & counter & " written")
End If
counter = counter + 1
Loop
End Sub
Function CalcWhitespace(rawStr As String, maxLen As Integer) As Integer
CalcWhitespace = maxLen - Len(rawStr)
End Function
Any Idea?