buenas, necesito ayuda, tengo la siguiente macro para exportar un fichero .txt a excel separados por columnas, como se puede observar en la instrucción "TextFileFixedColumnWidths = Array(21, 21, 31, 42, 30, 28, 20, 19, 20, 46, 31, 28, 22, _
21, 22, 17, 26, 28, 26, 28, 25, 27, 26, 27, 29, 20, 22, 30, 26, 29, 21, 29, 28, 30)", delimito la posición donde se debe de dividir, lo que ocurre que el fichero .txt cambia el tamaño de esas columnas. en la fila 2 de todos los ficheros .txt las columnas están indicadas por el signo "----", aunque cambién el tamaño. sería posible que el TextFileFixedColumnWidths inique la posición conforme esté el signo "---" en cada campo, en unos será 21, 21... y en otros 21, 25, etc.
".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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(21, 21, 31, 42, 30, 28, 20, 19, 20, 46, 31, 28, 22, _
21, 22, 17, 26, 28, 26, 28, 25, 27, 26, 27, 29, 20, 22, 30, 26, 29, 21, 29, 28, 30)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
gracias anticipadas.
Puedes intentar los siguientes pasos:
1.- Cambia la parte que dice .TextFileParseType = xlFixedWidth a .TextFileParseType = xlDelimited
2.- Elimina la parte que dice .TextFileFixedColumnWidths = Array(21, 21, 31, 42, 30, 28, 20, 19, 20, 46, 31, 28, 22, 21, 22, 17, 26, 28, 26, 28, 25, 27, 26, 27, 29, 20, 22, 30, 26, 29, 21, 29, 28, 30)
3.- Antes de EndWith agrega .TextFileOtherDelimiter = "----"
Esto debería funcionar.
Saludos!
Time to upgrade, it’s free!
Simply click on the top left of the extension and register to get access to 10 shortcuts. Your email will remain secure, but we might get in touch for your feedback!Programas y complementos para Microsoft Office
Visítame: https://cronytech.jimdofree.com/productos-y-servicios/
gracias Cronytech por contestar, pero no me vale ya que el signo "--" está en la 2ª linea. te pongo como serían los .txt.
nombre apellidos deporte
--------- ---------- ----------
en todos los txt la segunda línea tiene los signos y determinan la longitud del campo. he probado con tabulación, espacio, y no sale como deseo.