+ Reply to Thread
Results 1 to 10 of 10

Command word from excel

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Command word from excel

    I have a text file (*.txt) that I want to modify before using in excel. I want to replace the return carriage with (Char 13) as long as the return carriage is not proceeded by a certain code. The code is fixed but there is a lot of different variations to the code, so if someone could help me with only one variation, I hope I will be able to do the rest.

    So the macro should:

    1. open the txt file via Word
    2. replace the carriage breaks that are not proceeded by a code with (Char 13)
    3. save the txt file

    Example attached.

    Thanks.

    Abousetta
    Attached Files Attached Files
    Last edited by abousetta; 05-04-2010 at 05:17 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Command word from excel

    No need for Word you can use file I/O commands instead.

    Not 100% sure what the file output should have for those combined lines. If you use CHR(13) excel will convert to news rows again.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Command word from excel

    Hi Andy,

    Thanks. And as you correctly pointed out, in Excel is sends each new carriage return to a new row. What I was hoping for was to allow the contents to all go into one cell. When I recorded what I wanted using the vb recorder, I got this code:

    Please Login or Register  to view this content.
    Of course I typed this directly into excel but I think it will better illustrate what I am after.

    Thanks for your help.

    Abousetta

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Command word from excel

    Did you try changing the CHR(13) part of the code to CHR(10) ?

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Command word from excel

    Hi Andy,

    Sorry I have not responded sooner but I was off the grid for a while. Char(10) seems to work as you suggested and I finally got it working and tweeked the code just a bit to fit with the rest of the project. Even so I still have a few questions I hope you can help me with:

    Here is the code as it stands right now:
    Please Login or Register  to view this content.
    The changes so far are minor as I just added Application.GetOpenFilename and Application.GetSaveAsFilename and changed the headers to represent what truly comes from the database. All the headers are a fixed length of six characters, spaces or numbers. In the example above I used "AB - " for the first header and "** - " for the second header. Is there any way for me to modify the code that I don't have to put down all the possibilities. The reason I ask this is because I noticed that the code will replace all the return carriages until it finds the stopping header. What I have found is that this stopping header (i.e. the next header) may be different from one file to the next.

    The other question I have is when I try to save the new output file as the same name as the original file, it gives me error at this line of code:
    Please Login or Register  to view this content.
    Is there anything I can do to allow me to overwrite the original file?

    Thanks for all your help.

    Abousetta

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Command word from excel

    First, you can not use the same file name for input and output. You need to output to a differently named file.
    Once you complete the restructure of the file you can close both files and then use the KILL command to delete the original and the NAME to rename the revised output back to the original name.

    Handling different delimiters is really down to find something consistent with the header that you can use to identify each part of the record.

    Perhaps it is more simple in your case. Each record is say 7 rows long and you want to append rows 3, 4, 5, 6

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Command word from excel

    Thanks Andy. I will try to add the KILL and NAME commands and ask for advice if I can't make it work.

    As for the second part, thanks for modifying the code above, I will try it out and let you know.

    Thanks for all your help and advise.

    Abousetta

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Command word from excel

    Hi Andy,

    I am trying to make the code work when there are different headers but I just can't seem to make it happen.

    The headers are all constant in the sense that they all consist of 6 characters (with spaces), of which the last two are always a dash followed by a space (e.g. "AB - ", "XY -", "A1 - "). Each header represents something differernt, but in the end the all must be the same length. Is there anyway to use wildcards to replace the characters (e.g. ** or ??) so that the macro will run until it finds the next header. I have tried but all it does is erase the rest of the document. Also, unfortunaley the number of lines is variable based on original user input and so I cannot determine this a priori.

    Thanks for all your help.

    Abousetta

    P.S. I have modified the code below to work with the attached example.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Command word from excel

    How about

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Command word from excel

    Thanks Andy... It seems to work perfectly this time around.

    Best wishes,

    abousetta

+ 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