Macro importar archivos texto
Publicado por kunzho (1 intervención) el 14/01/2008 16:44:51
Hola! Tengo que hacer una macro que importe 100 archivos txt, en las pestañas que hay creadas en un archivo de excel.
Los archivos se llaman: 201.sol, 202.sol, 203.sol... hasta 300.sol (son archivos de texto con la extensión cambiada.
Las pestañas donde se tienen que copiar se llaman: 201, 202, 203....300
He grabado una macro de importación de los archivos y he modificado un poco el código, pero no me funciona!!!! Me podéis ayudar?
Os explico la macro: En primer lugar seleccionamos el directorio donde están los archivos, y luego he creado un bucle para que se vaya creando la ruta donde están los archivos, y se importen con las variables que yo quiero....
Sub ImportFiles()
Dim objShell As Object
Dim objFolder As Object
Dim strFolderFullPath As String
Dim i As Variant
Dim filepath As String
Set objShell = CreateObject("Shell.Application")
'oFolder = Shell.BrowseForFolder(Hwnd, sTitle, iOptions [, vRootFolder])
Set objFolder = objShell.BrowseForFolder(0, "Please select a folder", 0, "C:") 'SpecFolders.CSIDL_FAVORITES
If (Not objFolder Is Nothing) Then
'// NB: If SpecFolder= 0 = Desktop then ....
On Error Resume Next
If IsError(objFolder.Items.Item.Path) Then strFolderFullPath = CStr(objFolder): GoTo Here
On Error GoTo 0
'// Is it the Root Dir?...if so change
If Len(objFolder.Items.Item.Path) > 3 Then
strFolderFullPath = objFolder.Items.Item.Path & Application.PathSeparator
Else
strFolderFullPath = objFolder.Items.Item.Path
End If
Else
MsgBox "User cancelled": End
End If
Here:
MsgBox "You selected " & strFolderFullPath, vbInformation
'we have selected the path where are of our files
'i want to repeat the importation of the files that are in the folder.
Dim a As String
For i = 200 To 300
'here is the path were are my files.
myPath = strFolderFullPath
'my files are from 200.sol to 300.sol
'I use predefine sheets with the same name of the file, they are called 201, 202,...300
'complet path of my files.
Filename = i & ".sol"
MsgBox (Filename)
Sheets(i).Select
filepath = strFolderFullPath & Filename
MsgBox (filepath)
'now i want to import the files, they are a kind of txt files with another extension
'the space delimiter is the :
' i want to put all of them in the two first columns of all the sheets.
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;& filepath", Destination:= _
Columns("A:B"))
.Name = a
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ":"
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
End With
Next
End Sub
Los archivos se llaman: 201.sol, 202.sol, 203.sol... hasta 300.sol (son archivos de texto con la extensión cambiada.
Las pestañas donde se tienen que copiar se llaman: 201, 202, 203....300
He grabado una macro de importación de los archivos y he modificado un poco el código, pero no me funciona!!!! Me podéis ayudar?
Os explico la macro: En primer lugar seleccionamos el directorio donde están los archivos, y luego he creado un bucle para que se vaya creando la ruta donde están los archivos, y se importen con las variables que yo quiero....
Sub ImportFiles()
Dim objShell As Object
Dim objFolder As Object
Dim strFolderFullPath As String
Dim i As Variant
Dim filepath As String
Set objShell = CreateObject("Shell.Application")
'oFolder = Shell.BrowseForFolder(Hwnd, sTitle, iOptions [, vRootFolder])
Set objFolder = objShell.BrowseForFolder(0, "Please select a folder", 0, "C:") 'SpecFolders.CSIDL_FAVORITES
If (Not objFolder Is Nothing) Then
'// NB: If SpecFolder= 0 = Desktop then ....
On Error Resume Next
If IsError(objFolder.Items.Item.Path) Then strFolderFullPath = CStr(objFolder): GoTo Here
On Error GoTo 0
'// Is it the Root Dir?...if so change
If Len(objFolder.Items.Item.Path) > 3 Then
strFolderFullPath = objFolder.Items.Item.Path & Application.PathSeparator
Else
strFolderFullPath = objFolder.Items.Item.Path
End If
Else
MsgBox "User cancelled": End
End If
Here:
MsgBox "You selected " & strFolderFullPath, vbInformation
'we have selected the path where are of our files
'i want to repeat the importation of the files that are in the folder.
Dim a As String
For i = 200 To 300
'here is the path were are my files.
myPath = strFolderFullPath
'my files are from 200.sol to 300.sol
'I use predefine sheets with the same name of the file, they are called 201, 202,...300
'complet path of my files.
Filename = i & ".sol"
MsgBox (Filename)
Sheets(i).Select
filepath = strFolderFullPath & Filename
MsgBox (filepath)
'now i want to import the files, they are a kind of txt files with another extension
'the space delimiter is the :
' i want to put all of them in the two first columns of all the sheets.
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;& filepath", Destination:= _
Columns("A:B"))
.Name = a
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ":"
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
End With
Next
End Sub
Valora esta pregunta


0