Read data from txt files


/ Published in: Visual Basic
Save to your folder(s)

Read data from TXT files to import them in Excel environment


Copy this code and paste it in your HTML
  1. Sub ImportFromText(x As Integer, y As Integer, filename As String)
  2. Dim base As String
  3. Dim WS As Worksheet
  4. Dim WB As Workbook
  5.  
  6. Set WB = ThisWorkbook
  7. Set WS = WB.Worksheets("Results")
  8.  
  9. base = ThisWorkbook.PATH
  10. file_path = base & "\output\" & filename
  11. Open file_path For Input As #1
  12. r = x
  13. Do Until EOF(1)
  14. Line Input #1, Data
  15. v = Split(Data, vbTab)
  16. For i = 0 To 3 Step 1
  17. If r = x Then
  18. WS.Range("AA2").Offset(r, y + i) = v(i)
  19. Else
  20. WS.Range("AA2").Offset(r, y + i) = Round(CDbl(v(i)), 5)
  21. End If
  22. Next i
  23. r = r + 1
  24. Loop
  25. Close #1
  26. End Sub
  27.  
  28. Sub import_modified_files()
  29. Call ImportFromText(0, 0, "Stream number 1 - stream function 0,0000.txt")
  30. Call ImportFromText(0, 4, "Stream number 2 - stream function 0,2500.txt")
  31. Call ImportFromText(0, 8, "Stream number 3 - stream function 0,5000.txt")
  32. Call ImportFromText(0, 12, "Stream number 4 - stream function 0,7500.txt")
  33. Call ImportFromText(0, 16, "Stream number 5 - stream function 1,0000.txt")
  34. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.