I have this script that creates a new column for each event from a text file it works but I want to change the format so instead of new column it is a new row
NC = wsDATA.Cells(1, Columns.Count).End(xlToLeft).Columns + 1
I tried
NC = wsDATA.Cells(1, Rows.Count).End(xlDown).Rows + 1
Thanks
Dale
Here is the complete code
Sub ImportTextFiles()
Dim fPATH As String, fNAME As String
Dim NC As Long, wsDATA As Worksheet
Dim MyStr As String, MyArr As Variant, MyArr2 As Variant, MyArrD As Variant, MyArrT As Variant, a As Long
fPATH = "C:\data\logs\" 'path to text files, remember the final \ in this string
Set wsDATA = ThisWorkbook.Sheets("Sheet1") 'sheet to import data into
If MsgBox("Clear existing data before importing?" & vbLf & _
"(NO will append new data to existing data)", vbYesNo) = vbYes Then
wsDATA.UsedRange.Offset(, 1).Clear
NC = 2
Else
NC = wsDATA.Cells(1, Rows.Count).End(xlDown).Rows + 1
End If
Application.ScreenUpdating = False 'speed up macro execution
fNAME = Dir(fPATH & "*.log") 'get first text filename
'Do While Len(fNAME) > 0 'loop through files one at a time till all done
Open fPATH & fNAME For Input As #1
' Line Input #1, MyStr
Do While Not EOF(1)
Line Input #1, MyStr 'read in the text. For some reason all the lines of your
'text files are being read in a single line, so we split those
'lines into separate strings and evaluate them one at a time
'add the missing sections, if missing
'MyStr = Replace(MyStr, "keyMarkets=[[ACHP", "keyMarkets=[[H1RS : H= D= A=], [MRES : H= D= A=], [AHCP")
'remove special characters
' MsgBox (MyStr)
MyStr = Replace(MyStr, "/", "^")
MyArr2 = Split(MyStr, Chr(10)) 'split the string into separate lines
For a = LBound(MyArr2) To UBound(MyArr2) 'check one line at a time
If Left(MyArr2(a), 5) = "SALES" Then 'make sure the line starts with SALES
' If InStr(MyArr2(a), "MRES") = 0 Then MyArr2(a) = Replace(MyArr2(a), "AHCP", "MRES : H= D= A= AHCP")
' If InStr(MyArr2(a), "AHHT") = 0 Then MyArr2(a) = Replace(MyArr2(a), "OVUN", "AHHT| : H= A= OVUN")
' If InStr(MyArr2(a), "OUH1") = 0 Then MyArr2(a) = MyArr2(a) & " OUH1| : O= U="
MyArr = Split(MyArr2(a), "^") 'split the line into a second array
'MsgBox (MyArr(2))
wsDATA.Cells(1, NC).Value = MyArr(0)
MyArrT = Split(MyArr(0), "_")
wsDATA.Cells(2, NC).Value = MyArrT(2)
MyArrD = Split(MyArr(1), " ")
wsDATA.Cells(3, NC).Value = MyArrD(0)
wsDATA.Cells(4, NC).Value = MyArrD(1)
wsDATA.Cells(5, NC).Value = Replace(MyArr(2), "", "")
NC = NC + 1 'increment to next empty column
End If
Next a 'loop to next line in file
Loop
Close #1 'close the file when finished
fNAME = Dir 'get the next text filename
'Loop 'repeat process with next found file
wsDATA.Columns.AutoFit 'clean up appearance
Application.ScreenUpdating = True
'FORMAT the DATE and TIME fields
Rows("3:3").Select
Selection.NumberFormat = "m/d/yyyy"
Rows("4:4").Select
Selection.NumberFormat = "h:mm;@"
'ALIGN ALL COLUMNS LEFT
Cells.Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
Bookmarks