+ Reply to Thread
Results 1 to 24 of 24

How to export/program/macro from excel to .txt file?

  1. #1
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    How to export/program/macro from excel to .txt file?

    Good evening from Norway,

    I have a delicate issue that I need some help with. I have a spreadsheet with loads of data rows and columns that need to be exported to a .txt file for upload to a database. Hence, the spesific format is what I am struggeling with.

    The format needs to be like this:

    “Customerno” “Name” “Address” zip code” “zip code name” “Phone” ”MecCode” ”SalesCode”
    "134871" "MOLO KYST RESTAURANT" "VALLEVEIEN 1000" "3960" "STATHELLE" "99627181" "" "233"
    "129978" "BURGY SIRKUS SHOPPING" "FALKENBORGVEIEN 5" "7044" "TRONDHEIM" "97969552" "" "534"


    I have no idea of how to do this, but maybe one of you guys can help?

    Attached the spreadsheet with the data. If this is possible with a formula, it would be great to have it in column Q or R
    Attached Files Attached Files

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: How to export/program/macro from excel to .txt file?

    How about a macro to do it:
    Please Login or Register  to view this content.
    I did not know which column is the MecCode so I put it in column 17 which is actually blank. As you certainly know where is this information, you can change this in the loop part.
    It is not a sophisticated macro but it works.
    Hope this helps!
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Pierre,

    Thank you for your help. Unfortunately, I am not able to get the code to work.

    Here is the macro I made:

    Please Login or Register  to view this content.
    I removed the line related to "MecCode", as I have to make a separate macro in a different workbook for this line.

    Where is it going wrong? I am not able to write the data to the .txt file, but I get no error message.

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: How to export/program/macro from excel to .txt file?

    I tried your macro on my computer an the txt file was created with all data in it.
    I can't tell you what is going wrong.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    I noticed the headings you posted do not match with the workbook headings. Please post the column letters along with the heading names.

    You should also provide more details about what the code is not doing correctly. If there are any error messages include the error number.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi,

    So sorry to bother you, I discovered that the error was entirely mine. I created a .txt document on the designated output place, and the macro did not like to have a document already there with the desired name on it. It works like a charm!

  7. #7
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi,

    I know, it has been modified and luckily the macro provided above by p24leclerc works like a charm

    Thank you for your help!

  8. #8
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi,

    I have a similar challenge that I cannot figure out. I am trying to do exactly the same, but with different information, creating a new output file. The headings are in cell H1 to J3, and the actual data is downwards from H3:J3 to H2500:J2500

    The heading is as guessed, "MEC", "Customer" and "CCTR"

    I am not able to get this macro to work, the error code is "Compile Error, Syntax error".

    Here is the macro:

    Please Login or Register  to view this content.
    I appreciate any help, my VB skills is rather poor I'm afraid...

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    Why is each entry enclosed with the number 2 - chr(50) ???

  10. #10
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Leith Ross,

    As far as I could figure out, it represents the length of the characters...? If it is wrong, please feel free to correct. As I mentioned before, this is my first steps into VB so I am a real rookie in this field.

  11. #11
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Leith Ross,

    As far as I could figure out, it represents the length of the characters...? If it is wrong, please feel free to correct. As I mentioned before, this is my first steps into VB so I am a real rookie in this field.

  12. #12
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi again,

    I just modified the code a bit, and now the macro "works", but the output is not what I am looking for.

    Here is the macro:
    Please Login or Register  to view this content.
    And here is an example from the output file:

    "MEC" "Customer" "CCTR"
    "MEC" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" "MEC"
    "" "" "MEC" "Customer"
    "" "MEC" "Customer" "CCTR"
    "MEC" "Customer" "CCTR" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "" "" "" ""
    "MEC" "Customer" "CEL" "CCTR"
    "Customer" "CEL" "CCTR" ""
    "CEL" "CCTR" "" ""
    "CCTR" "" "" ""
    "" "" "" "95105218"
    "" "" "95105218" "POS Catalog"
    "" "95105218" "POS Catalog" "CCDN4705"
    "95105218" "POS Catalog" "CCDN4705" ""
    "95105218" "POS Catalog" "895802000" "CCDN4705"
    "POS Catalog" "895802000" "CCDN4705" ""
    "895802000" "CCDN4705" "" ""
    "CCDN4705" "" "" ""

    I am looking at an output with this desired format:

    "MEC" "Customer" "CCTR"
    "95909015" "Hunting - OOH" "CCDN4005"

    It seems that the macro is producing an output from cell A2 and downwards, in stead of my desired range from H3:J2500 ....
    Last edited by Absalon; 10-27-2014 at 02:43 PM.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    This data layout is different than the workbook you posted originally.

    If you have another workbook with raw data on it, it would help to troubleshoot any problems you are having.

  14. #14
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Leigh,

    That is correct. Here is an example of the sorted data that I need to have in the format specified earlier. Note that the position of the rows are exactly as in my original workbook.

    Example.xlsx

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    Thanks for the example file. I was able to create and test the macro below using it.

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Leith,

    Thank you so much, the macro works like a charm!!

  17. #17
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    You're welcome.

  18. #18
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    Do you have any more questions or can this post be marked solved?

  19. #19
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi again,

    Maybee you also can help me with the following:

    I would like to create a macro that takes the two output files that are stored on my desktop and ad them as attachments to an email. I do not want to send it, but it the macro could include the recepient as "xx@logistikkhuset.no"

    Is this possible?

  20. #20
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    If you are using Outlook I can write a macro to do it.

  21. #21
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi,

    Yes I am, I am using Outlook 2010 version.

    Thank you so much!

  22. #22
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    And for a newbie like me, where do I mark this thread as solved?

  23. #23
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to export/program/macro from excel to .txt file?

    Hello Absalon,

    You will need to change the subject, message, and the file paths in the macro to match what you will be sending.

    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    01-22-2014
    Location
    Norge
    MS-Off Ver
    Office 365
    Posts
    55

    Re: How to export/program/macro from excel to .txt file?

    Hi Leith Ross,

    I have another question for you if you have the time.

    I need to expand this macro to be valid for four columns in stead of three.

    Here is the code:

    Please Login or Register  to view this content.
    I get an error saying that the subscript is out of range, and the problem should be here: Print #1, """" & Data(1) & """ """ & Data(2) & """ """ & Data(3) & """ """ & Data(4) & """ """ & """"

    The last code was valid for

    Set Rng = Range("H1:J1")
    Set Rng = Range(Rng, Cells(Rows.Count, Rng.Column).End(xlUp))

    Would you please let me know what I am doing wrong here?

+ 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. Macro that will export a sheet to a .dat text file (Excel 2003)
    By CameronR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-17-2013, 08:47 AM
  2. Build a Macro that can go online and export a data file to excel?
    By WillyBanks in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2013, 10:58 PM
  3. Macro assistance needed for complex export/import excel file
    By dedavie in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-28-2013, 02:47 PM
  4. Macro to export from one excel file to another
    By SKaplan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-10-2011, 04:56 AM
  5. [Macro excel] How to create and export a sheet into a *.inp (or *.txt) file
    By MrKermit in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2006, 03:30 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