You are on page 1of 1

Sub Importar_Datos_a_Excel

Dim ResultStr As String, FileName As String, FileNum As Integer, Counter As Double

' FileName = InputBox("Please enter the Text File's name, e.g. test.txt") 'Ask User for File's Name
' If FileName = "" Then End 'Check for no entry
FileName = Application.GetOpenFilename(filefilter:="Archivo de texto (*.txt),*.txt", Title:="Elegir Archivo")
If FileName = "" Then End 'Check for no entry

FileNum = FreeFile() 'Get Next Available File Handle Number


Open FileName For Input As #FileNum 'Open Text File For Input

Application.ScreenUpdating = False 'Turn Screen Updating Off


Workbooks.Add template:=xlWorksheet 'Create A New WorkBook With One Worksheet In It

Counter = 1 'Set The Counter to 1


Do While Seek(FileNum) <= LOF(FileNum) 'Loop Until the End Of File Is Reached
Application.StatusBar = "Importing Row " & Counter & " of text file " & FileName 'Display Importing Row Number On Status Bar
Line Input #FileNum, ResultStr 'Store One Line Of Text From File To Variable
If Left(ResultStr, 1) = "=" Then 'Store Variable Data Into Active Cell
ActiveCell.Value = "'" & ResultStr
Else
ActiveCell.Value = ResultStr
End If

If ActiveCell.Row = 1000000 Then 'For Excel versions before Excel 97, change 65536 to 16384
ActiveWorkbook.Sheets.Add 'If On The Last Row Then Add A New Sheet
Else
ActiveCell.Offset(1, 0).Select 'If Not The Last Row Then Go One Cell Down
End If
Counter = Counter + 1 'Increment the Counter By 1
Loop 'Start Again At Top Of 'Do While' Statement

Close 'Close The Open Text File


Application.StatusBar = False 'Remove Message From Status Bar

End Sub

You might also like