Tengo un archivo en excel que debo convertir a .TXT

Tengo un archivo en excel que debo convertir a .TXT. El excel tiene varias columnas con diferentes formatos, y el TXT que debo crear tiene un determinado diseño, usando los campos del excel debo generar el txt, pero por ejemplo las longitudes no son iguales en el excel que el txt, y tampoco las columnas están en igual posición. Por favor me podrían ayudar para hacer esto en forma automática.? No encontré para insertar el excel, y el diseño del txt para que sea mas clara mi pregunta.

Necesito si me pueden dar una mano para poder generar algo en forma automatica.

1 Respuesta

Respuesta
1

Envíame tu archivo de excel con las explicaciones, también envíame el archivo txt con el resultado que esperas.

Gracias!!! ya te envié los archivos.

Esta es la macro para generar el archivo, prueba y me comentas.

Sub ConvertirATxt()
'Por.Dante Amor
    Application.ScreenUpdating = False
    Set h1 = Sheets("Hoja1")
    Set h2 = Sheets("Hoja2")
    Set h3 = Sheets("Hoja3")
    '
    h1.Cells.Copy
    h3.[A1].PasteSpecial Paste:=xlPasteValues
    h3.Cells.EntireColumn.AutoFit
    h3.Cells.NumberFormat = "General"
    'h3.rows1.Delete
    h3.Select
    u = h3.Range("A" & Rows.Count).End(xlUp).Row
    '
    Columns("R:R").Cut
    Columns("E:E").Insert Shift:=xlToRight
    '
    Columns("I:I").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    '
    For i = 2 To u
        Range("J" & i) = Int(Range("X" & i))
        Range("K" & i) = (Range("X" & i) - Int(Range("X" & i))) * 100
    Next
    Columns("K:K").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("K2:K" & u) = ","
    '
    cols = Array("F", "G", "H", "I", "M", "N", "O", "P", "Q", "R", "S")
    n = Array(2, 6, 1, 30, 1, 10, 1, 1, 10, 1, 6)
    For j = LBound(cols) To UBound(cols)
        For i = 2 To u
            If Cells(i, cols(j)) = "" Or Cells(i, cols(j)) = 0 Then
                Cells(i, cols(j)) = "'" & String(n(j), "0")
            End If
        Next
    Next
    Columns("T:T").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("V:V").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("X:X").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("Z:Z").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    'Columns("AB:AB").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    '
    Columns("AA:AA").HorizontalAlignment = xlRight
    '
    Columns("AB:AC").Delete Shift:=xlToLeft
    Rows(1).Delete
    h3.Cells.NumberFormat = "General"
    For i = 2 To 31
        col = h2.Cells(i, "C")
        lon = h2.Cells(i, "D")
        Columns(col).ColumnWidth = lon
        If h2.Cells(i, "G") <> "" Then
            Columns(col).NumberFormat = String(lon, "0")
        End If
    Next
    '
    Application.DisplayAlerts = False
    ruta = ThisWorkbook.Path
    Cells.Copy
    Workbooks.Add
    ActiveSheet.Paste
    ActiveWorkbook.SaveAs Filename:=ruta & "\prueba.prn", FileFormat:=xlTextPrinter, CreateBackup:=False
    ActiveWindow.Close
    MsgBox "Archivo prueba.prn generado", vbInformation, "Finalizado"
End Sub

Recuerda valorar la respuesta.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas