Copy multiple text files to different sheet of the same Excel file.

Source: Internet
Author: User

To meet this requirement, You need to copy multiple files to different sheet of the same Excel file. Each text file has one sheet, and the sheet name uses the name of the text file, using VBA can easily implement this function, but it is still a little unfamiliar with the VB syntax and is backed up here.

 Sub  Importtextfiles ()  ''  Import text files to a Excel file.  '  '  Dim  Filepath, filenamefilepath = "  D: \ items \  "  Filename = Dir (Filepath +"  *. Txt  "  )  Do   While Filename <> ""  Sheets. Add after: = Worksheets (worksheets. Count)  With Activesheet. querytables. Add (connection: = "  Text;  " & Filepath & Filename _, destination: = Range ("  $ A $1  "  ). Fieldnames = True  . Rownumbers = False  . Filladjacentformulas = False  . Preserveformatting = True  . Refreshonfileopen = False  . Refreshstyle =Xlinsertdeletecells. savepassword = False  . Savedata = True  . Adjustcolumnwidth = True  . Refreshperiod = 0  . Textfilepromptonrefresh = False  . Textfileplatform = 936  . Textfilestartrow =1  . Textfileparsetype = Xldelimited. textfiletextqualifier = Xltextqualifierdoublequote. textfileconsecutivedelimiter = True  . Textfiletabdelimiter = True  . Textfilesemicolondelimiter = False  . Textfilecommadelimiter = False  . Textfilespacedelimiter = True  . Textfilecolumndatatypes = Array ( 2 , 1 , 2 , 1  ). Textfiletrailingminusnumbers = True  . Refresh backgroundquery: = False      End   With  Range (  " A1: d1  "  ). Select selection. Font. Bold = True  Activesheet. Name = Left (Filename, Len (Filename )- 4  ) Filename = Dir  ()  Loop  End sub 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.