I have routine tasks which require pulling data from an obsolete SPC package and putting it into Excel. The SPC software will only export into a text file.
I have put pieced together a script to import this into excel, break it into different sheets and do a little bit of cleaning on it. I am sure this is not the most efficient code. Any advice on cleaning this up, error handling or streamlining?
Option Explicit
Public Sub ImportTextFile(FName As String, Sep As String)
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Dim ws As Worksheet
Dim SheetNumber As Long
Const C_START_SHEET_NAME = "Sheet1"
SheetNumber = 1
RowNdx = 1
Set ws = ActiveWorkbook.Worksheets(C_START_SHEET_NAME)
Application.ScreenUpdating = False
'On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
'This section added to create new sheets for empty lines
If WholeLine = "" Then
SheetNumber = SheetNumber + 1
Set ws = ActiveWorkbook.Worksheets.Add(after:=ws)
RowNdx = 1
End If
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
SheetNumber = SheetNumber + 1
Wend
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub
Sub DoTheImport()
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Sep = vbTab 'Application.InputBox("Enter a separator character.", Type:=2)
If Sep = vbNullString Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ImportTextFile FName:=CStr(FileName), Sep:=CStr(Sep)
Call RemoveBlankRowsColumns
Call CleanChar
Call TabName
Call A2_Format
End Sub
Private Sub RemoveBlankRowsColumns()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
On Error Resume Next
ws.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Next
End Sub
Private Sub TabName()
Dim ws As Worksheet
Dim myString As String
Dim newString As String
For Each ws In Worksheets
With ws
If .Range("A1").Value <> "" Then .name = .Range("A1").Value
End With
Next ws
End Sub
Private Sub CleanChar()
Dim rCell As Range
Dim rRng As Range
Dim rCol As Range
Dim ws As Worksheet
Dim myString As String
Dim newString As String
For Each ws In Worksheets
With ws
Set rRng = ws.Range("A1:A50")
For Each rCol In rRng.Columns
For Each rCell In rCol.Rows
myString = rCell.Value
newString = Replace(Replace(myString, ":", ""), """", "")
rCell.Value = newString
Next rCell
Next rCol
End With
Next ws
End Sub
Private Sub A2_Format()
Dim rCell As Range
Dim rRng As Range
Dim rCol As Range
Dim ws As Worksheet
Dim myString As String
Dim newString As String
Dim splitTarget As Variant
Dim a As Integer
For Each ws In Worksheets
With ws
Set rRng = ws.Range("A2")
For Each rCol In rRng.Columns
For Each rCell In rCol.Rows
myString = rCell.Value
newString = Replace(Replace(Replace(myString, "(", ","), ")", ","), "=", ",")
splitTarget = Split(newString, ",")
For a = 0 To UBound(splitTarget)
ws.Cells(2, a + 1).Value = splitTarget(a)
Next a
Next rCell
Next rCol
End With
Next ws
End Sub