20-07-2013, 03:21
Boa noite.
Estou neste momento a tentar programar uma macro em VBA em excel para importar ficheiros de texto para uma folha de excel e que delimita o texto por colunas automaticamente. Mais detalhadamente pretendia:
- os ficheiros importados que fiquem colocados a partir da coluna H do excel;
- correr macro e que abra uma janela para escolher o ficheiro de texto e que fique delimitado em colunas automaticamente sem ter que delimita-las manualmente;
Já estive à procura e encontrei o seguinte exemplo:
Sub ImportTextFile()
Dim DestBook As Workbook, SourceBook As Workbook
Dim DestCell As Range
Dim RetVal As Boolean
' Turn off screen updating.
Application.ScreenUpdating = False
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = ActiveCell
' Show the Open dialog box.
RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
' If Retval is false (Open dialog canceled), exit the procedure.
If RetVal = False Then Exit Sub
' Set an object variable for the workbook containing the text file.
Set SourceBook = ActiveWorkbook
' Copy the contents of the entire sheet containing the text file.
Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
' Activate the destination workbook and paste special the values
' from the text file.
DestBook.Activate
DestCell.PasteSpecial Paste:=xlValues
' Close the book containing the text file.
SourceBook.Close False
End Sub
Mas aqui eu tenho que delimita-las manualmente..... e são colocadas na célula que está seleccionada e não na coluna H como pretendo.
Tenho que simular mais de 2600 operações e isto ajudava imenso para concluir a minha tese de mestrado.
Cumprimentos,
Bruno Duarte
Estou neste momento a tentar programar uma macro em VBA em excel para importar ficheiros de texto para uma folha de excel e que delimita o texto por colunas automaticamente. Mais detalhadamente pretendia:
- os ficheiros importados que fiquem colocados a partir da coluna H do excel;
- correr macro e que abra uma janela para escolher o ficheiro de texto e que fique delimitado em colunas automaticamente sem ter que delimita-las manualmente;
Já estive à procura e encontrei o seguinte exemplo:
Sub ImportTextFile()
Dim DestBook As Workbook, SourceBook As Workbook
Dim DestCell As Range
Dim RetVal As Boolean
' Turn off screen updating.
Application.ScreenUpdating = False
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = ActiveCell
' Show the Open dialog box.
RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
' If Retval is false (Open dialog canceled), exit the procedure.
If RetVal = False Then Exit Sub
' Set an object variable for the workbook containing the text file.
Set SourceBook = ActiveWorkbook
' Copy the contents of the entire sheet containing the text file.
Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
' Activate the destination workbook and paste special the values
' from the text file.
DestBook.Activate
DestCell.PasteSpecial Paste:=xlValues
' Close the book containing the text file.
SourceBook.Close False
End Sub
Mas aqui eu tenho que delimita-las manualmente..... e são colocadas na célula que está seleccionada e não na coluna H como pretendo.
Tenho que simular mais de 2600 operações e isto ajudava imenso para concluir a minha tese de mestrado.
Cumprimentos,
Bruno Duarte