Crear archivo de TXT VBA Excel

Anónimas
2021-09-08T22:21:06+00:00

Buenas Tardes tengo el siguiente código para crear un archivo de texto que esta delimitado por espacios, intente grabarlo como PRN pero el máximo es de 240 caracteres, he agregado unas columnas mas a mi archivo pero por algún motivo las columnas adicionales las graba en la parte de abajo

'Genera Archivo txt

f = 1 

For I = 6 To 84 

        If IsEmpty(Sheets("Resumen").Cells(1, 4)) = False Then 

            num1 = CStr(Format(Sheets("Resumen ").Cells(I, 2).Value, "#0;-#0;#0;#0")) 

            num2 = CStr(Format(Sheets("Resumen ").Cells(I, 3).Value, "#0;-#0;#0;#0")) 

            num3 = CStr(Format(Sheets("Resumen ").Cells(I, 4).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num4 = CStr(Format(Sheets("Resumen ").Cells(I, 5).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num5 = CStr(Format(Sheets("Resumen ").Cells(I, 6).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num6 = CStr(Format(Sheets("Resumen ").Cells(I, 7).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num7 = CStr(Format(Sheets("Resumen ").Cells(I, 8).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num8 = CStr(Format(Sheets("Resumen ").Cells(I, 9).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num9 = CStr(Format(Sheets("Resumen ").Cells(I, 10).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num10 = CStr(Format(Sheets("Resumen ").Cells(I, 11).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num11 = CStr(Format(Sheets("Resumen ").Cells(I, 12).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num12 = CStr(Format(Sheets("Resumen ").Cells(I, 13).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num13 = CStr(Format(Sheets("Resumen ").Cells(I, 14).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num14 = CStr(Format(Sheets("Resumen ").Cells(I, 15).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num15 = CStr(Format(Sheets("Resumen ").Cells(I, 16).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num16 = CStr(Format(Sheets("Resumen ").Cells(I, 17).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num17 = CStr(Format(Sheets("Resumen ").Cells(I, 18).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num18 = CStr(Format(Sheets("Resumen ").Cells(I, 19).Value, "#0.00;-#0.00;#0.00;#0.00")) 

            num19 = CStr(Format(Sheets("Resumen ").Cells(I, 20).Value, "#0;-#0;#0;#0")) 

            num20 = CStr(Format(Sheets("Resumen ").Cells(I, 21).Value, "#0;-#0;#0;#0")) 

            Sheets("Data ").Cells(f, 1).Value = Sheets("Resumen ").Cells(I, 1).Text 

            Sheets("Data ").Cells(f, 2).Value = num1 

            Sheets("Data ").Cells(f, 3).Value = num2 

            Sheets("Data ").Cells(f, 4).Value = num3 

            Sheets("Data ").Cells(f, 5).Value = num4 

            Sheets("Data ").Cells(f, 6).Value = num5 

            Sheets("Data").Cells(f, 7).Value = num6 

            Sheets("Data ").Cells(f, 8).Value = num7 

            Sheets("Data ").Cells(f, 9).Value = num8 

            Sheets("Data ").Cells(f, 10).Value = num9 

            Sheets("Data ").Cells(f, 11).Value = num10 

            Sheets("Data ").Cells(f, 12).Value = num11 

            Sheets("Data ").Cells(f, 13).Value = num12 

            Sheets("Data ").Cells(f, 14).Value = num13 

            Sheets("Data ").Cells(f, 15).Value = num14 

            Sheets("Data ").Cells(f, 16).Value = num15 

            Sheets("Data ").Cells(f, 17).Value = num16 

            Sheets("Data ").Cells(f, 18).Value = num17 

            Sheets("Data ").Cells(f, 19).Value = num18 

            Sheets("Data ").Cells(f, 20).Value = num19 

            Sheets("Data ").Cells(f, 21).Value = num20 

            f = f + 1 

        End If 

    Next I 

    Sheets("Data MPV").Activate 

    ActiveWorkbook.SaveAs Filename:="D:\" + Sheets("Resumen ").Cells(3, 1).Text + ".txt", FileFormat:=xlTextPrinter, CreateBackup:=False 

'Cierra Libro

ActiveWorkbook.Close (False) 

End Sub

RESULTADO

Microsoft 365 y Office | Excel | Para la casa | Windows

Pregunta bloqueada. Esta pregunta se migró desde la Comunidad de Soporte técnico de Microsoft. Puede votar si es útil, pero no puede agregar comentarios o respuestas ni seguir la pregunta.

0 comentarios No hay comentarios
{count} votos

2 respuestas

Ordenar por: Muy útil
  1. Anónimas
    2021-09-09T05:48:59+00:00

    Lo que te ocurre es que lo que quieres imprimir no cabe en el ancho de la hoja (fíjate en el número de columnas).

    Puedes mejorar el código escribiendo antes del FOR

    With Worksheets("Data ")

    y despues del next

    End with.

    No necesitas cargar las variables Numx,

    .Cells(f, 2).Value = CStr(Format(Sheets("Resumen ").Cells(I, 2).Value, "#0;-#0;#0;#0"))

    0 comentarios No hay comentarios
  2. Héctor Miguel 71,590 Puntos de reputación
    2021-09-09T05:42:46+00:00

    usar el "FileFormat:=xlTextPrinter" genera archivos *.prn (aunque fuerces la EXTension a ".txt") y es la causa del limite de 240 caracteres por linea

    mira por aqui:

    0 comentarios No hay comentarios