+ Reply to Thread
Results 1 to 3 of 3

VBA code to copy data from Excel to multiple text files

  1. #1
    Registered User
    Join Date
    03-12-2020
    Location
    Singapore
    MS-Off Ver
    2019
    Posts
    7

    VBA code to copy data from Excel to multiple text files

    Hi,

    I would like to the have the VBA codes to copy data from MS Excel, paste into Notepad and save as text file format (.txt).
    Please refer to steps below. I have an MS Excel file with 2 worksheets: Fruits and Vegetables.

    1. In worksheet: Fruits, Col A to F are the metadata information and Col G to L are data from year 2000 to 2005. Row 1 refers to the headers and row 2 to 4 refers to the data. Some cells may be empty. Sample data format (I use comma to represent different cells):
    [Row1] Product code, Product sub-code, Product, Product name, Unit, Details, Jan2000, Feb2000, Mar2000, Apr2000, May2000, Jun2000
    [Row2]Fruits01, Fruits01.01, Fruits, Apple, Kg, Max 1000kg, 500.1, 100.3, 300.2, 450.4, 100.6, 250.8
    [Row3]Fruits01, Fruits01.02, Fruits, Orange, Kg, , 200, 400, 450, 200, 150, 420
    [Row4]Fruits01, Fruits01.03, Fruits, Watermelon, Tonnes, Stock in Jan2000. , 10, 30, 25, 20, 40, 15

    2. Can I have the VBA codes to copy data from each row (row 2 onwards) of the worksheet and paste into Notepad and save as 1 txt file per row. The filename will follow col B value. There will be 3 files.

    The format of text file for row 2 is:
    [Row1] Fixed text
    [Row2] Fixed text|insert formula to count number of data point from col G onwards|col B value
    [Row3] |col A value: col C value|col B value: col D value|col E value
    [Row4] cell G1 value displayed in YYYY MMM|col G value
    [Row5] cell G1 value displayed in YYYY MMM|col H value
    [Row6] cell G1 value displayed in YYYY MMM|col I value
    [Row7] cell G1 value displayed in YYYY MMM|col J value
    [Row8] cell G1 value displayed in YYYY MMM|col K value
    [Row9] cell G1 value displayed in YYYY MMM|col L value

    In the text file (Fruits01.01.txt), the data format will be:
    Food
    Company|6|Fruits01.01
    |Fruits01:Fruits|Fruits01.01:Apple|Kg|Max 1000Kg.
    2000 Jan|500.1
    2000 Feb|100.3
    2000 Mar|300.2
    2000 Apr|450.4
    2000 May|100.6
    2000 Jun|250.8

    3. The process will repeat for the 2nd worksheet till end of the file.

    4. In the actual excel file, there will be multiple worksheets, X number of data columns from col G onwards and Y number of rows from row 2 onwards. The number of columns and rows will vary in different worksheets.

    Appreciate your assistance to advise on the VBA codes.
    Thanks.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA code to copy data from Excel to multiple text files

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-12-2020
    Location
    Singapore
    MS-Off Ver
    2019
    Posts
    7

    Re: VBA code to copy data from Excel to multiple text files

    Hi,

    Thanks for the reply. I have attached the excel file and a sample text file for your reference, please.
    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Code to copy specific contents from 2 text files to separate excel columns
    By Miteshkg in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 02-22-2016, 03:31 AM
  2. Copy data to excel from multiple *.txt files
    By dispro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2015, 08:14 AM
  3. Open multiple files and copy data is slow, code after end of loop not working - help?
    By hadamhiram in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2014, 11:52 PM
  4. Copy Data from Multiple Text Files to Single Worksheet
    By Oded_moshe in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-29-2013, 12:26 PM
  5. Replies: 9
    Last Post: 02-12-2013, 04:15 PM
  6. VBA Code to open text files and copy information over to an excel sheet
    By kenadams378 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2012, 12:18 PM
  7. Macro to copy multiple text files in excel and merger with same family data
    By Narensshekhawat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2012, 04:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1