Excel vba: Saving data from a spreadsheet in .txt format

0

Good afternoon. I have the macro below and it already does almost what I want (it creates a txt file with the data of the Historico worksheet, always rewriting the file, that is, it does not create several copies), however this History sheet contains soccer team names and when finding strange names such as "Śląsk Wrocław, Górnik Łęczna, Wisła Płock" the macro hangs and the message appears: Run-time error '5': Argument or invalid procedure call

Option Explicit

Sub CreateTXT ()

'tem que ativar a referência => Microsoft Scripting Runtime

Dim NombreArchivo, RutaArchivo As String
Dim obj As FileSystemObject
Dim tx As Scripting.TextStream
Dim Ht As Worksheet
Dim i, j, nFilas, nColumnas As Integer

NombreArchivo = "Batch"
RutaArchivo = ActiveWorkbook.Path & "\" & NombreArchivo & ".txt"

Set Ht = Worksheets("Historico")
Set obj = New FileSystemObject
Set tx = obj.CreateTextFile(RutaArchivo)

nColumnas = Ht.Range("A1", Ht.Range("A1").End(xlToRight)).Cells.Count
nFilas = Ht.Range("A2", Ht.Range("A2").End(xlDown)).Cells.Count

For i = 1 To nFilas

    If Ht.Cells(i + 1, 4).Value <> "" Then

    For j = 1 To nColumnas
        tx.write Ht.Cells(i + 1, j).Value  'É nesta parte onde a macro trava
        If j < nColumnas Then tx.write "|"
    Next j

    tx.writeLine

    End If

Next i

End Sub

    
asked by anonymous 22.05.2017 / 20:03

1 answer

0

If you do not care that these names do not come into your txt you can simply add it at the beginning of the code:

On Error Resume Next

If this is not the case, try to review the method you are using to write to txt, I usually use

Print #1, "Texto qualquer"

We can talk more, anything.

    
23.05.2017 / 13:38