+ Reply to Thread
Results 1 to 19 of 19

Macro to convert data into a text file in special format

  1. #1
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Macro to convert data into a text file in special format

    Hi all

    I am hoping someone can help with a macro to do the following.....

    I have my data in in this format:

    Date FX Rate Conv Code
    30/10/2013 1.1681 EUR
    29/10/2013 1.1717 EUR
    28/10/2013 1.1711 EUR

    I need to take each row in the file and convert into this format.

    DC
    C
    EUR
    30102013
    <>
    <>
    /
    1.1681
    <>
    <>
    <>
    <>
    DC
    C
    EUR
    29102013
    <>
    <>
    /
    1.1717
    <>
    <>
    <>
    <>
    DC
    C
    EUR
    28102013
    <>
    <>
    /
    1.1711
    <>
    <>
    <>
    <>

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    Something like:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Macro to convert data into a text file in special format

    or
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    I get run-time error 1004 on this
    Method "Range" of object_Global failed.

  5. #5
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    Something like:
    Please Login or Register  to view this content.
    I get run-time error 1004 on this
    Method "Range" of object_Global failed.

  6. #6
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by nilem View Post
    or
    Please Login or Register  to view this content.
    This macro did work until it got to the dates section. 09/10/2013 was entered as 9102013 and 08/10/2013 was entered as 8102013.

    Any way to correct this?

    Great solution though - thanks so much!

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Macro to convert data into a text file in special format

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    For what it's worth, mine should have been:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    For what it's worth, mine should have been:
    Please Login or Register  to view this content.
    Thanks for that- this worked perfectly.

    Is there a way to adapt this macro so that it can also do the following:

    In cell F1 I have the following values "ASL,CPL,ALL,APL,ACO,CZZ"
    I want to be able to loop through each three letter code in cell F1 to create data for each entity in the list.

    Sub macro_1()
    Dim fs, fil, count
    Set fs = CreateObject("Scripting.filesystemobject")
    Set fil = fs.createtextfile("E:\test.txt")
    For count = 2 To Range("A" & Rows.count).End(xlUp).row
    fil.writeline "SS"
    fil.writeline "XXX" - THIS IS THE THREE LETTERS IN CELL F1
    fil.writeline "LA"
    fil.writeline "DC"
    fil.writeline "C"
    fil.writeline Range("C" & count)
    fil.writeline Replace(Range("A" & count).text, "/", "")
    fil.writeline "<>"
    fil.writeline "<>"
    fil.writeline "/"
    fil.writeline Range("B" & count)
    fil.writeline "<>"
    fil.writeline "<>"
    fil.writeline "<>"
    fil.writeline "<>"
    Next
    End Sub

    This would then achieve exactly what I needed. As currently I have to import that file for each entity where as this script would create the necessary script for each entity.

    Many thanks

    S

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    Please Login or Register  to view this content.
    Hi Yudlugar

    I get that error again - error 1004 on this row

    fil.writeline Range("C" & count)

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    sorry, it's because the first iteration of the loop is 0. I'm not sure I full understand though, did you want to loop through every row, for every instance of XXX? or do you have the same amount of XXX's as you do rows, in the case of the former:
    Please Login or Register  to view this content.
    for the latter:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    sorry, it's because the first iteration of the loop is 0. I'm not sure I full understand though, did you want to loop through every row, for every instance of XXX? or do you have the same amount of XXX's as you do rows, in the case of the former:
    Please Login or Register  to view this content.
    for the latter:
    Please Login or Register  to view this content.
    Thanks for your help.

    For every XXX in field F1 I should have exactly the same rows.

    Neither of these seem to work though.

  14. #14
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    Could you upload a sample workbook please, click go advanced then manage attachments.

  15. #15
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    Could you upload a sample workbook please, click go advanced then manage attachments.

    SUN FX Rates.xlsm

    See the attached excel file

  16. #16
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    Please Login or Register  to view this content.
    I tested that and it seemed ok. Made a few errors in the variable definitions last time.

  17. #17
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    Please Login or Register  to view this content.
    I tested that and it seemed ok. Made a few errors in the variable definitions last time.
    It's not quite working as yet. It seems to be including the column C heading of "CONVCODE" instead of looping through column C from row 2 onwards.

  18. #18
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Macro to convert data into a text file in special format

    One more go:
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    04-19-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    33

    Re: Macro to convert data into a text file in special format

    Quote Originally Posted by yudlugar View Post
    One more go:
    Please Login or Register  to view this content.
    Works brilliantly! Thanks very much for your help.

    Have a great evening.

+ 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. Please help to get a macro to convert without format file to excel file.
    By excel_boy1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2013, 12:27 PM
  2. [SOLVED] Convert text file to CSV and extract data loop macro help
    By Savan87 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-23-2012, 04:42 AM
  3. Export Excel data to txt file in special format
    By tan123 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-05-2010, 05:19 PM
  4. Replies: 1
    Last Post: 08-30-2009, 11:26 AM
  5. Replies: 2
    Last Post: 11-16-2005, 08:25 AM

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