Try this:
Option Explicit
Sub ImportAndFormat()
Dim fName As String
Dim LR As Long
'Select an import file
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "C:\2011\Import\"
.AllowMultiSelect = False
.Filters.Add "Text Files", "*.txt", 1 'default
.Filters.Add "All Files", "*.*" 'everything
.Show
If .SelectedItems.Count > 0 Then fName = .SelectedItems(1) Else Exit Sub
End With
'Import data
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
Destination:=Range("A1"))
.Name = "CellCalls"
.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 = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'Format new table
Range("B1") = "TYPE"
Range("C1") = "NUMBER"
Range("D1") = "NAME"
Range("E1") = "CALLTYPEV2"
Range("F1") = "OTHERPARTYTYPE"
Range("G1") = "ENDCAUSE"
Range("H1") = "ENDCAUSESIP"
Range("I1") = "ENDCAUSESTRING"
Range("J1") = "ENDLOCATION"
Range("K1") = "CALLSTARTTIME"
Range("L1") = "CONNSTARTTIME"
Range("M1") = "CALLENDTIME"
Range("N1") = "DURATION"
Range("O1") = "NEWVOICEMAIL"
LR = Int(Range("A" & Rows.Count).End(xlUp).Row / 15)
With Range("B2:O" & LR)
.FormulaR1C1 = _
"=SUBSTITUTE(INDEX(C1, ((ROW(R[-1]C[-1])-1)*15)+COLUMN(R[-1]C)),R1C&""="","""")"
.Value = .Value
End With
Range("A:A").Delete xlShiftToLeft
Columns.AutoFit
End Sub
Edit the InitialFilename string to the path where your files are most likely to be.
Also, I set the filter to text files, you can edit that to whatever extension your data file usually has.
Bookmarks