+ Reply to Thread
Results 1 to 10 of 10

VBA code to copy data from Excel by rows 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 by rows 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 Jan 2000 to Jun 2000. Row 1 refers to the headers and row 2 to 4 refers to the data. Some cells may be empty. Please refer to sample data format in the attachment.

    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

    There will be 3 text files generated from Sheet: Fruits. Please refer to samples in the attachments.

    3. The process will repeat for the 2nd worksheet: Vegetables (annual data) till end of the file.

    4. In the actual excel file, there will be multiple worksheets (monthly or annual data), 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
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

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

    Quote Originally Posted by hmlaw View Post
    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 haven't seen too many questions here get answered when ""steps"" are given. Not sure why. But regardless, what you need to know is that you can't officially copy data from excel to notepad by way of the screen interface of your machine. however, the options to do this with code that I'm aware of are:

    => possibly using the SENDKEYS() methods in VBA.
    => saving your excel file with a .txt extension.
    => using I/O streaming to throw your data from excel into an un-named file with a .txt extension that is created and/or appended to with VBA code.

    for the last point, the functions you want to research are as follows:

    => PRINT()
    => WRITE()

    https://docs.microsoft.com/en-us/off...printstatement

    https://docs.microsoft.com/en-us/off...writestatement

    you can also create/append/open .txt files with this coding language by using what is called the fileSystemObject. as you can see from the following articles, these methods also make use of the functions I mentioned above:

    https://docs.microsoft.com/en-us/off...extfile-method

    https://docs.microsoft.com/en-us/off...extfile-method

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

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

    Quote Originally Posted by hmlaw;5397261
    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
    This should do for the uploaded workbook.
    Please Login or Register  to view this content.

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

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

    Hi jindon,

    Thanks for very much for the reply and the VBA codes, it worked well.

    I have made further changes to the Excel workbook with the following. Sorry for the changes.

    1. New columns added between the original col A to F. Col A to J are the metadata information and Col K to P are data from Jan 2000 to Jun 2000.
    2. For monthly worksheet, date in source workbook are in MMMYYYY format. Can we convert it to YYYY MMM in the text output files?
    I have added the frequency in col F. May I know if VBA codes be able to convert the date based on frequency col?
    For monthly worksheets, the periods to be converted to YYYY MMM in the text files and for non-monthly workbook while it will remain as same date value as the workbook.
    3. To display value as 250.0 in the text file instead of 250. In the workbook, the number of decimal places to be defaulted to 1 decimal place, but some of the worksheets are in 2 or more decimal places.
    Can the VBA codes be able to tell the number of decimal places in the Excel and display the same in the output files.
    4. The filename of output files will follow col C (Product ID): e.g. Fruits12345678.txt
    5. Can we specify the path of the output text files in another folder different from the Excel workbook, e.g. C:\test\123\

    Please refer to the updated workbook and text file in the attachment.

    May I have the modified VBA codes, please.
    Thank you for your assistance.

    Hi vba_php, thanks for your reply and info.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

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

    Please Login or Register  to view this content.

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

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

    Hi jindon,

    Thanks again for the codes. I noticed that row 3 of the text file has been displayed as |Fruits01.01|Apple|. It was working fine in the 1st version.
    Please advise on the new codes to display as |Fruits01.01:Apple|.

    Thanks.
    Last edited by hmlaw; 09-20-2020 at 09:56 PM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

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

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

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

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

    Hi jindon,

    The codes worked well and thanks very much for your assistance and patience.

    May I seek your guidance to explain how does each line of the code works? I would like to gain an understanding of VBA codes so that I can learn to modify them if there is any change in format in future. Thank you.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

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

    Happy to help you understand my code, but...

    Do a step debug.
    while you are in vbe:
    1) [View] - [Local Window]
    2) Click somewhere in the code.
    3) as you hit F8, the code executes one line and you will see all the variables in Local Window.

    Try to get the meaning of each lines for yourself first, using VBA Help/any web site.
    Then ask specific line(s) that you don't understand, if you still don't understand.

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

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

    Hi jindon,

    Noted, thank you.

    If you know of any guide or websites suitable for beginners, please let me know too.
    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. VBA code to copy data from Excel to multiple text files
    By hmlaw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2020, 01:15 PM
  2. [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
  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