bankwing

Excel VBA import multiple text files

Sep 9th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. Sub Sample()
  2. 'File: c:\file1.txt,c:\file2.txt
  3. Dim Filelist() As Variant
  4. Filelist = Array("file1", "file2")
  5.  
  6. Dim wbI As Workbook, wbO As Workbook
  7. Dim wsI As Worksheet
  8.  
  9. Set wbI = ThisWorkbook
  10. Dim Inx As Long
  11.  
  12. For Inx = LBound(Filelist) To UBound(Filelist)
  13. Debug.Print Filelist(Inx)
  14. '-----Import for Sheet
  15. Set wsI = wbI.Sheets(Filelist(Inx)) '<~~ Sheet where you want to import
  16.  
  17. Set wbO = Workbooks.Open("C:\" & Filelist(Inx) & ".txt")
  18.  
  19. wbO.Sheets(1).Cells.Copy wsI.Cells
  20. wbO.Close SaveChanges:=False
  21.  
  22. Next
  23.  
  24.  
  25. End Sub
Advertisement
Add Comment
Please, Sign In to add comment