Hi everyone,
I have cobbled together an Excel macro which imports a text file from a folder, strips away blank spaces, text and other characters and then saves the imported text file to a new Excel file, using the name of the text file as the new Excel filename. I'd like to be able to loop this such that this process will continue for however many text files are within the source folder.
Help would be much appreciated.
Thanks,
Ian
I have cobbled together an Excel macro which imports a text file from a folder, strips away blank spaces, text and other characters and then saves the imported text file to a new Excel file, using the name of the text file as the new Excel filename. I'd like to be able to loop this such that this process will continue for however many text files are within the source folder.
Help would be much appreciated.
Thanks,
Ian
VBA Code:
Sub ImportText()
Dim fName As String
Application.ScreenUpdating = False
' Select Text file dialog box
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fName = "False" Then Exit Sub
' Import Text file using Import Wizard
With Worksheets("Sheet2").QueryTables.Add(Connection:="TEXT;" & fName, _
Destination:=Worksheets("Sheet2").Range("$A$1"))
.Name = "Mark Sheet"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.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)
.TextFileFixedColumnWidths = Array(9, 21, 9, 7, 7, 8, 7, 9, 9, 6, 7, 8, 12)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
' Delete brackets and underline
Sheets("Sheet2").Select
Cells.Replace What:="(", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:=")", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="_________ ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Autofit columns
Cells.Select
Selection.Columns.AutoFit
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
Range("A1").Select
End With
' Delete rows with blanks and text characters from row 15 to row 1000
Range("A15:A1000").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
Range("A15:A1000").Select
Selection.SpecialCells(xlCellTypeConstants, 2).Select
Selection.EntireRow.Delete
' Copy signature line from Sheet1
Sheets("Sheet1").Select
Range("A50:I53").Select
Selection.Copy
Range("A1").Select
Sheets("Sheet2").Select
'Paste signature line at end of Sheet2 document
Dim lastRow As String
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 6
Range("A" & lastRow).Select
Selection.PasteSpecial
Range("A1").Select
' Delete data connections
Call deleteConnections
' Save Sheet2 as Excel workbook with imported text filename
Dim strFolder As String
strFile = fName & ".xlsx"
Application.DisplayAlerts = False
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:=strFile, _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
Application.ScreenUpdating = True
'Delete data in Sheet2
Cells.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
'Return to Sheet1
Sheets("Sheet1").Select
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Sub deleteConnections()
For i = 1 To ActiveWorkbook.Connections.Count
If ActiveWorkbook.Connections.Count = 0 Then Exit Sub
ActiveWorkbook.Connections.Item(i).Delete
i = i - 1
Next i
End Sub