+ Reply to Thread
Results 1 to 10 of 10

Troubleshooting code for export to text file.

  1. #1
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208

    Troubleshooting code for export to text file.

    I was wondering if someone would be willing to help me troubleshoot this code. I got the code from here: http://www.cpearson.com/excel/ImpText.aspx

    I'm sure that it works fine. But I was trying to modify it to fit my needs. I can step through it fine, but it doesn't generate the text file for the cells I'm selecting. At least, I think I'm selecting them. I might have goofed up the cell ranges.

    I want to select 15 rows in a single column. So each cell will go on it's own line in a text file, and I don't want any character separation. In the macro that calls the function(ExportToTextFile), I changed
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    So here's the full code that I'm trying to debug. I have a separate macro that calls this function:

    Please Login or Register  to view this content.
    It appears that nothing happens when I get to this line of
    Please Login or Register  to view this content.

    If anyone can take the time to explain to me what I'm doing wrong, I would really appreciate it.

    Thanks,
    -gshock
    Last edited by gshock; 11-03-2008 at 07:46 PM. Reason: Solved - Thanks to shg!!! Kudos

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    If AppendData is false, you'll never open a file, yet the code continues. That's bad.

    I can't envision why you would set the row and column numbers as you do. And if SelectionOnly is not true, you wouldn't set them at all.

    Why do you have the OnError statement? The first step if you get errors is to find out why. Start by taking it out.

    Take out Application.ScreenUpdating = False too, until the code is stable.

    Edit: And in fairness to Chip, that code is modified substantially from the link. Did you try his code as-is?
    Last edited by shg; 10-31-2008 at 04:54 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208
    Hey shg, nice to see you again.

    I got this from another post I had made. I don't fully understand the code. The author said that AppendData was for adding text to the end of an existing file. Since I'm creating entirely new files, I figured that I didn't need it. So I made it false.

    I was guessing at how to set the row and column numbers. The intent is do get this working for a single column, and then get it to iterate for every instance of a particular value at the top of each column. I was trying to break it into pieces because it's too much to do all at once.

    SelectionOnly should be true, because I want only a select group of cells, and not the entire worksheet.

    OnErrorStatement is there just because I didn't know enough about it to know what would happen if I took it out.

    I didn't try his code as is because I'm trying to do columns instead of rows. But now that you ask it, I realize it's just a 1 column by x row set of data. Now I see the error of changing that. Didn't occur to me at the time.

    Yeah, I modified the code, but I wanted to make sure I gave proper credit to where the original came from.

    Thanks for your comments.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Yeah, but you took out the wrong code. Go back to the original, pass the input parameters correctly, and step through it.

    After you get it working, edit as desired.

  5. #5
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208

    Export to text file.

    Shg, thanks for your patience with me on this one.

    Ok, I followed your suggestion and copied Chip's original code exactly. For my needs, I created a loop, and I'm calling the function 4 times for each iteration of the loop. I commented out the Application.Screenupdating=False, and the OnError statements as you suggested from your last post. When I step through the code, it seems to be grabbing the text that I want, but it's not creating the text file. Can you help me figure out what's wrong? Thanks again for your time. I'm very grateful for all of your help.



    Please Login or Register  to view this content.

    Regards,
    -gshock
    Last edited by gshock; 11-03-2008 at 12:40 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I just tried a small example and it worked find.

    Do you rellay intend to open the file for Append (add your stuff to the end)?

    These loop is unnecessary, arent't they?
    Please Login or Register  to view this content.
    Just
    Please Login or Register  to view this content.
    ... but it's not creating the text file.
    Sure you're looking in the right directory? The file is created immediately on execution of the Open statement.

  7. #7
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208

    Wink Is it boring being right all the time?

    Quote Originally Posted by shg View Post
    I just tried a small example and it worked fine.
    shg, You're a GENIUS!! You're right, I wasn't looking in the right directory.

    Once again, you have exposed my complete ineptitude with VBA!
    Darn you. Nevertheless, if it weren't for your generosity, I don't know where I would be. Seriously, thanks for your help. I really appreciate it.

    Notice the code below clearly indicates where the file should created. I just checked, and it's there, just like you said.

    Please Login or Register  to view this content.
    But I also have some code that allows the user to choose the file location. I got that part working, but I forgot to link the two pieces of code.

    Please Login or Register  to view this content.
    How can I match these two pieces up? I want the user to select the file location/folder, and then build the file name based off the value in a particular cell and the heading for each column.

    Can I do something like this?
    Please Login or Register  to view this content.
    The reason I want to use this naming convention is so that I can track the changes from case to case. For example, Parameter_A, cases 1 to 50; Parameter_B, cases 1 to 50; etc ... And I'm trying to get the naming convention to match. Each parameter has various inputs, which is what I'm exporting to text.

    I noticed that it's appending the file. And it looks like the loop is causing it, just like you said. I'll debug and see what I come up with.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    That question is unrelated to this thread, so how about starting a new one?

    BTW, as a general comment, when you get a versatile piece of code like that that does more than you need, hacking it to your lesser requirements is usually a bad idea. You may need more of the functionality on another occasion.

    And as a courtesy, it's nice to put an acknowledgement of the author and a link to the source, especially if you share the code within your organization.

  9. #9
    Forum Contributor
    Join Date
    03-04-2008
    Location
    Ohio
    MS-Off Ver
    Office 2010
    Posts
    208
    shg,

    EDIT: Problem solved. With a little ingenuity, I managed to accomplish what I wanted. Thanks again.


    DisregardNo problem. I'll be happy to post another thread. Sorry to get off topic.

    I'm not sure I understand what you mean about hacking code for to lesser requirements. Which part are you referring to?

    I've pasted a link to Chip's code in the comments of my macro. I just didn't include it in the code I've posted. Your point is well taken

    Thanks for the help.

    -gshock
    Last edited by gshock; 11-03-2008 at 07:45 PM.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I'm not sure I understand what you mean about hacking code for to lesser requirements.
    I mean your original attempt to take out what you didn't need.

    When an MVP posts code on their web site (versus a forum), it's usually pretty golden. If you modify it without knowing exactly what you're doing, you may mess it up. In the best case, it simply won't work (as here). In the worst case, you will introduce a subtle and occasional error that will have you pulling your hair out. Just use it and forget it until you reall need to change it.

    That's all I meant. Glad we have you mostly sorted now.

+ 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