Saltar al contenido
View in the app

A better way to browse. Learn more.

Ayuda Excel

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Exportar 100 líneas a .txt

publicado

Hola que tal,

Tengo el siguiente código que me ayuda a exportar una columna cada 100 filas y guardar lo en una carpeta en formato .txt pero cada vez que exporto al final de la línea 100 se agrega un espacio en blanco, me pueden ayudar a quitarlo y que solo queden las 100 líneas que necesito.

Adjunto código:

Sub ExportarTXT()

Dim mPath$, iniCell$, i&, LR&, Vec, j%, iniTime!, R%

iniCell = "$G$2"

iniTime = Timer
mPath = ThisWorkbook.Path & "\Txt\"

With CreateObject("Scripting.FileSystemObject")
On Error Resume Next: .GetFolder(mPath).Delete True: On Error GoTo 0
.GetFolder(ThisWorkbook.Path).subFolders.Add "Txt"
End With

LR = Cells(Rows.Count, Range(iniCell).Column).End(xlUp).Row

For i = Range(iniCell).Row To LR Step 100
Vec = Cells(i, Range(iniCell).Column).Resize(100)
R = 1 + R
Open mPath & Format(R, "0000") & ".txt" For Output As #1
For j = 1 To 100
If Vec(j, 1) = "" Then Exit For
Print #1, Vec(j, 1)
Next
Close
Next
MsgBox "Proceso terminado en: " & Format(Timer - iniTime, "0.00 seg")
End Sub

 

Featured Replies

publicado
  • Autor
En 24/3/2023 at 15:15 , Abraham Valencia dijo:

Adjunta al menos una parte de tus datos para poder probar y ver directamente cuál es el problema. 

Hola amigo,

Te adjunto una hoja de trabajo. Ss.

Exportar_TXT.xlsm

publicado

Mi propuesta.

Podrías modificar un poco el código como te lo presento a continuación donde además el Print con el punto y coma (;) después del valor de la última línea, evita que se escriba una línea adicional en el archivo de texto.

Algo así:

Sub ExportarTXT()

    Dim mPath As String, iniCell As String, i As Long, LR As Long, Vec, j As Long, iniTime As Double, R As Long

    iniCell = "$G$2"
    iniTime = Timer
    mPath = ThisWorkbook.Path & "\Txt\"

    With CreateObject("Scripting.FileSystemObject")
        On Error Resume Next: .GetFolder(mPath).Delete True: On Error GoTo 0
        .GetFolder(ThisWorkbook.Path).subFolders.Add "Txt"
    End With

    LR = Cells(Rows.Count, Range(iniCell).Column).End(xlUp).Row

    For i = Range(iniCell).Row To LR Step 100
        Vec = Cells(i, Range(iniCell).Column).Resize(100)
        R = 1 + R
        Open mPath & Format(R, "0000") & ".txt" For Output As #1
        For j = 1 To UBound(Vec)
            If Vec(j, 1) = "" Then Exit For
            If j = UBound(Vec) Then
                Print #1, Vec(j, 1);
            Else
                Print #1, Vec(j, 1)
            End If
        Next
        Close
    Next
    MsgBox "Proceso terminado en: " & Format(Timer - iniTime, "0.00 seg")

End Sub

Lo estuve probando y me ha funcionado bien, creo cumple con lo que estás buscando.

Saludines.

publicado
  • Autor
En 27/3/2023 at 16:44 , Israel Cassales dijo:

Te dejo el libro con el que hice pruebas.

Saludines_2

Exportar_TXT.xlsm 16.46 kB · 1 descarga

Gracias amigo, lo he probado y veo que ya no me agrega una línea adicional al final de los .txt exportados.

Ss.

Archivado

Este tema está ahora archivado y está cerrado a más respuestas.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.