Advertisement
mdelatorre

Import text file into Excel with VBA

May 23rd, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ImportData()
  2.  
  3.      Filename = InputBox("Enter the file path")
  4.     'Filename = the full path to Your txt file eg."C:\MyDocuments\Mytxt.txt"
  5.    Open Filename For Input As #1
  6.     Do While (Not EOF(1))
  7.         ' In this case the file is delimited by , and contains several lines
  8.    ' Read the file one line at the time
  9.    Input #1, Streng
  10.  
  11.     'Input streng into and array
  12.    StrArray = Split(Streng, ",")
  13.     Call WriteToExcel(StrArray)
  14.     Loop
  15.     Close #1
  16. End Sub
  17.  
  18. Sub WriteToExcel(StrArray)
  19.      For J = LBound(StrArray) To UBound(StrArray)
  20.        'Do what you want to do with the data
  21.    Next J
  22. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement