+ Reply to Thread
Results 1 to 12 of 12

VBA Code to export to .txt file - Needs tweaking

  1. #1
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    VBA Code to export to .txt file - Needs tweaking

    Hi all,

    Apologies for making my first post on here a request for help - I have been trying to tweak a VBA code to get the result I am after, however failing miserably.

    I've got an Excel file that I am using to calculate some approximations for anisotropic material data for use with FEA. All of the data lies on Sheet1. The data that I want to export starts on row 30, and ends on row 33.

    The actual cells that I want exporting are:

    A30
    A31-H31
    A32-H32
    A33-E33

    I need the output seperated by commas. My current code is below (taken off the net):


    Please Login or Register  to view this content.
    Current problems:

    1. Exports the whole sheet, including data above/below the section I want to export.
    2. Exports blank cells (ie. B30-H31 and G33-H33) and prints ", , ," to .txt file.
    3. I dont really want to offer the user the option of choosing a seperator - Ideally I would like it to be fixed as a comma but still offer the option of choosing file name and location.

    The text file needs to be exported in the same 'order' as the cells are on the sheet (ie. A30, <new line>A31-H31,<new line>A32-H32,<New line>,A32-F32.)

    Any help would be much appreciated!

    Kind regards

    Tom
    Last edited by tomjackuk; 02-08-2012 at 08:48 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to export to .txt file - Needs tweaking

    Please attach the excel sheet that you are using.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Code to export to .txt file - Needs tweaking

    example.xls

    Example sheet is attached. The values in red are what I want to export to a text file in the format:


    *ELASTIC, TYPE = ANISOTROPIC,
    5000, 4600, 5000, 3840, 3840, 2000, 0, 0,
    0, 1500, 0, 0, 0, 0, 1500, 0,
    0, 0, 0, 0, 200, 0,

    Thanks for the response!

  4. #4
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Code to export to .txt file - Needs tweaking

    Hey guys,

    After spending yesterday afternoon trying to figure it out, I am still struggling. I would be most appreciative if someone had a spare 5 mins to help me out

    Kind regards

    Tom

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to export to .txt file - Needs tweaking

    Use this code to create the .txt file
    Please Login or Register  to view this content.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Code to export to .txt file - Needs tweaking

    Please use codetags (see the forum rules)

    Please Login or Register  to view this content.



  7. #7
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Code to export to .txt file - Needs tweaking

    Thanks guys.

    First post is edited to include code tags, apologies.

    arlu - When i run your code, a new Excel workbook is made and in cell A1 I the data that I need. Rather than opening in a new excel file and pasting the data into one cell, I would like a pop up box to appear asking the user where to save the .txt file to and to give it a name. Then I would like it to save to the specified file.

    snb - This method seems a lot simpler. The text file that is written out using your code writes everything out all on one line, seperated by commas. Unfortunately, it needs to be seperated by lines too (in the same manner that they are seperated in rows on the Excel file, as shown below). I would like to add in a pop up box for user to specify a location and file name too. Would this be possible?

    *ELASTIC, TYPE = ANISOTROPIC,
    5000, 4600, 5000, 3840, 3840, 2000, 0, 0,
    0, 1500, 0, 0, 0, 0, 1500, 0,
    0, 0, 0, 0, 200, 0,


    Thanks in advance for the help guys

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Code to export to .txt file - Needs tweaking

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to export to .txt file - Needs tweaking

    Updated code here -
    Please Login or Register  to view this content.
    Close the excel file and then open it from the saved location. You will find it in .txt format.

  10. #10
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Code to export to .txt file - Needs tweaking

    Gents, thanks again for the responses. A few comments:

    snb - Using that modified code, the text file still includes commas where there are empty cells (as shown below):


    *ELASTIC, TYPE = ANISOTROPIC,,,,,,,
    12400,1800,12400,1420,1420,2700,0,0
    0,2300,0,0,0,0,2300,0
    0,0,0,0,5300,0,,


    Also, would it be possible to change the 'input' box to a more conventional type 'explorer' input box where the user can browse folders?


    Arlu - Same things happens with your code - the 'blank commas' are still there. Also, after the user enters the filename to save it as, a file is not actually created. An 'unsaved' excel file is created with a name different to the one which the user typed in the input box, and the user then has to go through the saving process again, selecting text file type. Ideally the process would flow something like:

    User clicks macro button > User enters file name to save to and presses 'save' > then, either the user is return to the excel sheet OR the text file is automatically opened in notepad.

    Thanks again for the help guys, I really do appreciate it!

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VBA Code to export to .txt file - Needs tweaking

    'Gents' : How do you know ???

    Try to avoid the need for userinput as much as possible.
    Why not storing the file in a predetermined folder ?

  12. #12
    Registered User
    Join Date
    02-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: VBA Code to export to .txt file - Needs tweaking

    I don't know, my bad.

    The need for the user input is because different people will be using the macro, and everyone likes to store their files in their own 'special place'. There are 13 network locations mapped on my machine, and two hard disks: I know for a fact that my drive 'letters' are not going to be the same as the other users of this file!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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