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

Asked

Viewed 2,259 times

1

Good afternoon. I have the macro below and it already does almost what I want (creates a txt file with the data of the Historico spreadsheet, always rewriting the file, ie, does not create several copies), but this Historico spreadsheet, contains names of football teams and when finding strange names like: "Śląsk Wrocław , Górnik Łęczna , Wisła Płock" the macro hangs and appears the message: Runtime error '5': Invalid argument or procedure call

Option Explicit

Sub Creartxt()

'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

1 answer

1

If you don’t mind that these names don’t enter your txt you can simply add 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.

  • 1

    Good morning Leandro. These names have to enter, because they are the names of the teams. How would I "fit" the code: Print #1, "Any text" into the code I already have? this above code is at the point I want, only with that flaw in relation to letters of other alphabets

Browser other questions tagged

You are not signed in. Login or sign up in order to post.