+ Reply to Thread
Results 1 to 24 of 24

Macro for save as as TXT

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Macro for save as as TXT

    Hi,

    I have use a macro to general text and add rows, but I would like to macro to also Save the result from excel to a text file. Can anyone help me to be able to automatically generate the .txt file into same directory after running the macro?
    Also, when applying the first macro, is it possible to also do:
    If character count is not 9 character, then add "_" underscore to data until it reaches 9 characters?

    Here is the macro and the final text file that should be like;
    HELP.xlsm

    HELP.txt
    Thanks anyone for help.
    Last edited by calvinle; 03-13-2014 at 11:56 AM.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,725

    Re: Macro for save as as TXT

    Nice solution Tinbendr. This is better than building the file as a spreadsheet then saving it as .txt. If you try to save it as .txt, then Excel will put quotes around any fields containing a comma, even if you are saving it as tab-delimited.

    calvinle, why are you using Chr codes for these characters: " ( )
    Seems like the code would be more readable if you just used those characters.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    @6StringJazzer: That excel will generate a code that will be useful for me to use in another platform. That is why I need the character " ( ). In the generated macro using excel, vba does not accept " ( ) in the vba code, it has to be char(x)...isnt it?

    @Tinbendr : Generating directly from excel will create those quote around field with comma, and I though I wud have complication about the vbtab but you even add it too. Thank you very much! You really help me!! Thanks again and very very much!

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    The macro works perfectly, however, I would like to know the possibility to add more $Temp from other cell to the line.
    If I have data in cell B, C, D, E, and I want them to be include in some parameter of the result, how should I process it?
    Here is an example of result that I want it to be..

    HELP.xlsmHELP.xlsm.txt

    Thanks.

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Ur fantastic!! Thank you so much!!

  8. #8
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Is it possible to add more field to the script? or can you explain how this really work on adding more Temp$?
    I want to add more detail to the scripting such as name, etc. But whenever I need it, I will just associate a Temp#$ to the script. I have tried to change so it starts by A5 because my new data started by A5 but it doesnt work.
    Here is the example:

    Example
    HELP.xlsm
    Result required
    HELP.txt

    Also, this file will be saved in the same folder, but what if I want to force it to be save in C:\FILE (if no folder if FILE, it will make one)

    A really big thanks for the help..

  9. #9
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Just use a cell reference in the data list. So, in B7 enter =C1.

    Change the number one to whatever the data starting row you want.

    Please Login or Register  to view this content.
    Then the cell will be handled by Temp2$.

  10. #10
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Sorry, I did not clearly explain the result. I want it to be included in each line of the generated result.

    Please Login or Register  to view this content.
    But the Temp3$, I want to associate it to the cell B2.
    So I can add Temp4$ to B3, and so on..

    Thanks.

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I guess I figure it out
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Another question about using this macro is about date.

    I am currently adding a date to a cell, and I would like to know when exporting to text, it becomes:
    28/03/2014, but I would like to make it become: 28MAR2014 instead. I have formatted the cell itself, but when exporting to text, it's still in 28/03/2014. How to force the cell to be in that DDMMMYYYY?

    I am using this code:
    Please Login or Register  to view this content.
    What should I add to this code to force the format date?

    Thanks.

  13. #13
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Please Login or Register  to view this content.
    The extra parentheses around the range is unnecessary.

  14. #14
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I did try this, but I didnt get any result, but now it's working. However, the date in B3 is:
    28mar2014, but when exporting to text, it become 28Mar1487?? Why is that?

  15. #15
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Sorry, nevermind, was inputting the wrong format code MMDDDYYYY instead of DDMMMYYYY. My bad..

  16. #16
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Yep, that'll do it.

    Working with dates in Excel can be a headache. Many Excel MVP's have written extensively on the subject. Here's a link to one of my favorites.

  17. #17
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi Tinbendr,

    I have changed the save location of the script to be save in C:\MACRO, but when the folder does not exist, the macro won't work. How can I mod this part to make it prompt up to ask where to save if the designate folder cannot be found?

    Thanks.

  18. #18
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    Something like:

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi,

    Sorry for asking again, but what if this time I want to do this way, what should I modify in the code to associate each cell right beside the Temp1$ separately?

    Please Login or Register  to view this content.
    HELP.xlsmHELP.xlsm.txt

    Thanks..

  20. #20
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    I'm not understanding what you need.

    In #5, you asked that all columns from B to the end, be concatenated and inserted on the [press] line.

    I answered that with #6.

    Now you want more info added to the next line? Where does this data come from?

    Or are you wanting to breakout the data across each line?

    Col A Col B Col C Col D
    3198 MOUSE CAT ELEPHANT

    Temp$ = Col B
    Temp2$ = Col C
    Temp3$ = Col D
    Temp4$= ???
    Last edited by Tinbendr; 04-08-2014 at 08:51 AM.

  21. #21
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    Hi. Exactly. I want to breakout the data across each line. Because I realise that instead of concatenate, I can just out them all together in 1 cell instead.

    I want to breakout exactly as you stated in ur topic.

    Thanks!

  22. #22
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Macro for save as as TXT

    OK, try this.
    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    It is possible to not use IF in this function?

    My data will all have Column A, B, C, D and E, so I think we can eliminate that IF function no?
    A B C D E
    334 DOG CAT COX CHILD
    243 CAT DOG CHILD COX
    322 COX CAT CHILD DOG

    What do you think?

  24. #24
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro for save as as TXT

    I mod it to this way and it seems to work, but can you check if it's good still?

    Please Login or Register  to view this content.

+ 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. how to specify a file save path to a save macro
    By Solidstan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-04-2013, 05:33 PM
  2. Macro to disable SAVE and SAVE AS
    By Charmymay in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-04-2013, 10:06 AM
  3. Macro with cell reference as a name, but prompt for save location and save as csv
    By tomham in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-15-2012, 06:21 PM
  4. [SOLVED] Macro to save sheets as different workbooks on save
    By BridgeCat in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2012, 08:10 AM
  5. where can i find macro codes for various functions such print, save and save as
    By fellayaboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-23-2011, 02:06 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