Como Abrir Archivo .txt mediante un macro y delimitado por el símbolo "^"

Quisiera conocer el código para una macro que haga lo siguiente:

1. Abrir un primer archivo .txt en el mismo archivo de la macro (ya que siempre que se abre un archivo .tx t me queda en un nuevo libro). El parámetro para separar es "^", así:

2. Lo segundo que requiero hacer es abrir un segundo archivo .txt y me lo ubique debajo de la información cargada en el paso anterior ya que tienen la mismo tamaño en columnas (este sin incluir los títulos)

Es esto posible, la verdad he intentado con el grabador de macros pero no me da lo que requiero.

1 respuesta

Respuesta
1

Te anexo la macro.

Ejecuta la macro Abrir_Txts.

Cambia "algo1.txt" y "algo2.txt" por los nombres de tus 2 archivos

Sub Abrir_Txts()
' Por Dante Amor
' Carga archivos de texto
'
    Application.ScreenUpdating = False
    Set h = Sheets("Hoja8")
    h.Cells.Clear
    '
    ruta = ThisWorkbook.Path & "\"
    arch1 = "algo1.txt"                 'Poner los nombres de archivo
    arch2 = "algo2.txt"
    u = 1
    Call Carga_txt(h, ruta, arch1, u)
    u = h.Range("A" & Rows.Count).End(xlUp).Row + 1
    Call Carga_txt(h, ruta, arch2, u)
    '
    Application.ScreenUpdating = True
End Sub
'
Sub Carga_txt(h, ruta, arch, u)
    With h.QueryTables.Add(Connection:= _
        "TEXT;" & ruta & arch, Destination:=h.Range("A" & u))
        .Name = "algo"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierNone
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "^"
        .TextFileColumnDataTypes = Array(2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

Hola Dante, como estas??

De antemano agradezco tu ayuda, siempre es muy util.

te cuento que hice las modificación de los nombres como me lo dijiste y adicional cambie hoja 8 por hoja 1.  Y al ejecutar la macro paso paso trabaja muy bien para cargar el primer archivo pero me esta presentando un problema al momento de cargar el segundo archivo, cuando hace este paso borra toda la información la información, a continuación te presento los cambios y los pantallas de como queda.

Sub Abrir_Txts()
' Por Dante Amor
' Carga archivos de texto
'
Application.ScreenUpdating = False
Set h = Sheets("Hoja1")
h.Cells.Clear
'
ruta = ThisWorkbook.Path & "\"
arch1 = "Base Actual.txt" 'Poner los nombres de archivo
arch2 = "Base historica.txt"
u = 1
Call Carga_txt(h, ruta, arch1, u)
u = h.Range("A" & Rows.Count).End(xlUp).Row + 1
Call Carga_txt(h, ruta, arch2, u)
'
Application.ScreenUpdating = True
End Sub
'
Sub Carga_txt(h, ruta, arch, u)
With h.QueryTables.Add(Connection:= _
"TEXT;" & ruta & arch, Destination:=h.Range("A" & u))
.Name = "algo"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierNone
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "^"
.TextFileColumnDataTypes = Array(2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Estos son los archivos .txt (los señalados)

al momento de cargar el Primer archivo

al momento de cargar el segundo archivo

no se que puede estar pasando, muchas gracias por toda tu ayuda

La macro me funciona sin problemas.

Tengo que revisar tus archivos para ver qué problemas tienen y también los cambios que le hiciste a la macro.

Envíame tus 2 archivos txt y el excel con tu macro

Mi correo [email protected]

En el asunto del correo escribe tu nombre de usuario “Ruben Guevara Jaramillo

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas