• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

copy specific from a text file to excel sheet using vba

alex gomes

New Member
I am trying to extract certain data from a text file(Source) into an Excel spreadsheet
I want copy the value from these two following line
TestTime Avg: 74.9451
TestTime StdDev: 3.1523


Attached sample txt file and required output excel file

Really appreciate your help!!!!
 

Attachments

  • Source.txt
    5.1 KB · Views: 15
  • Destination.xlsx
    8.4 KB · Views: 12
Alex

Firstly, Welcome to the Chandoo.org Forums

I have made an assumption that the data is always in the same format and the two lines you want are always the last two lines

If the above is true, then
Code:
Sub Sample()
  Dim MyData As String, strData() As String
  Dim tArr As Variant
   
  Open "C:\Users\mypcname\Desktop\Source.txt" For Binary As #1
  MyData = Space$(LOF(1))
  Get #1, , MyData
  Close #1
  strData() = Split(MyData, vbCrLf)
   
  tArr = Split(strData(UBound(strData, 1) - 1), " ")
  Range("A1") = tArr(2)
  tArr = Split(strData(UBound(strData, 1)), " ")
  Range("A2") = tArr(2)
   
End Sub

Will work

If not let me know
 
Thanks for your reply

No actual file is like this only,please go though the attached doc.
I want to copy every value in the line which is starting with
TestTime Avg:
TestTime StdDev:
there are multiple lines which is starting with these two
 

Attachments

  • Source.txt
    40.4 KB · Views: 9
Based on poor explanation, just mod this demonstration to your need :​
Code:
Sub Demo()
    V = ThisWorkbook.Path & "\Source.txt"
    If Dir(V) = "" Then Beep: Exit Sub
    F% = FreeFile
    Open V For Input As #F
    V = Filter(Filter(Split(Input(LOF(F), #F), vbNewLine), vbTab), "TestTime")
    Close #F
    If UBound(V) < 0 Then Beep: Exit Sub
    ReDim VA#(0 To UBound(V), 0)
    For F = 0 To UBound(V):  VA(F, 0) = Val(Split(V(F), vbTab)(1)):  Next
    With Worksheets(1).Cells(1)
        .CurrentRegion.Clear
        .Resize(F).Value = VA
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
Thanks For the reply And sorry deepak for creating new thread

This is the code i modified, But this is working partially

Code:
Sub Demo()
  V = ThisWorkbook.Path & "C:\Users\subhajit\Desktop\data.txt"
  If Dir(V) = "" Then Beep: Exit Sub
  F% = FreeFile
  Open V For Input As #F
  V = Filter(Filter(Split(Input(LOF(F), #F), vbNewLine), vbTab), "TestTime Avg")
  Close #F
  If UBound(V) < 0 Then Beep: Exit Sub
  ReDim VA#(0 To UBound(V), 0)
  For F = 0 To UBound(V):  VA(F, 0) = Val(Split(V(F), vbTab)(1)):  Next
  With Worksheets(1).Cells(1)
  .CurrentRegion.Clear
  .Resize(F).Value = VA
  End With
  V = ThisWorkbook.Path & "C:\Users\subhajit\Desktop\data.txt"
  If Dir(V) = "" Then Beep: Exit Sub
  F% = FreeFile
  Open V For Input As #F
  V = Filter(Filter(Split(Input(LOF(F), #F), vbNewLine), vbTab), "TestTime StdDev")
  Close #F
  If UBound(V) < 0 Then Beep: Exit Sub
  ReDim VA#(0 To UBound(V), 0)
  For F = 0 To UBound(V):  VA(F, 0) = Val(Split(V(F), vbTab)(1)):  Next
  With Worksheets(1).Cells(2)
  'CurrentRegion.Clear = False
  .Resize(F).Value = VA
  End With
End Sub

Actual_Result.xlsx is what i am getting after running this code
but what i want is Expected_Result.xlsx

Plz go through the attached documents
Plz Help!!
 

Attachments

  • Source.txt
    92.2 KB · Views: 12
  • Actual_Result.xlsx
    7.4 KB · Views: 9
  • Expected_Result.xlsx
    7.2 KB · Views: 11
Last edited by a moderator:
First, initial post must be crystal clear and complete …

If you ask for piece of a global need, it will take ages !
Code:
Sub Demo()
    V = ThisWorkbook.Path & "\Source.txt"
    If Dir(V) = "" Then Beep: Exit Sub
    F% = FreeFile
    Open V For Input As #F
    V = Filter(Filter(Split(Input(LOF(F), #F), vbNewLine), vbTab), "TestTime")
    Close #F
    If UBound(V) < 0 Then Beep: Exit Sub
    ReDim VA#(1 To (UBound(V) + 1) \ 2, 1 To 2)
    For F = 1 To UBound(V) Step 2
        If Not V(F) Like "*[#]IND" Then
                  L% = L% + 1
            VA(L, 1) = Val(Split(V(F - 1), vbTab)(1))
            VA(L, 2) = Val(Split(V(F), vbTab)(1))
        End If
    Next
    With Worksheets(1).Cells(1)
        .CurrentRegion.Clear
        .Resize(L, 2).Value = VA
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
Back
Top