Como puedo importar datos de un archivo .txt a una hoja de Excel mediante un macro

Mi problema es que tengo unos datos en un archivo .txt de esta forma:

11

12

13

14

Necesito mediante un macro leer cada valor y guardarlo en un arreglo para asi imprimirlo en mi hoja de excel.

1 respuesta

Respuesta
2

Te paso la macro que necesitas:

Sub textoaxls()
'Por Macro_leo
Dim fileName
fileName = Application.GetOpenFilename("Text Files (*.txt),*.txt")
If fileName <> "False" Then
Sheets("Hoja1").Select
On Error GoTo Salir
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & fileName _
, Destination:=Range("A1"))
.Name = "fileName"
.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 = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
On Error GoTo 0
Exit Sub
Salir:
Sheets("Hoja2").Select
MsgBox "Se canceló la Importación"
End If
Sheets("Hoja3").Select
End Sub

La macro la pones en un modulo

Al ejecutarla abre un directorio y buscas el archivo txt

Luego lo ingresa en la Hoja1 desde A1 en adelante

Si esto resuelva tu consulta no olvides finalizarla

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas