+ Reply to Thread
Results 1 to 8 of 8

VBA: Read cell number and write it in correct place in a .txt

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    VBA: Read cell number and write it in correct place in a .txt

    Hi all,

    I need help in doing a VBA macro. I have one Excel file where I have from B1 to B100 cells with a number (Area). Each Area is of a place number. So I have:
    Please Login or Register  to view this content.
    The .txt that I have is something like this:
    Please Login or Register  to view this content.
    As you can see each 'sacr1' .. 'sacr3', etc corresponds to the place number 1 and 3 in the Excel file.

    So what I would like, it's a VBA macro that could read the place area in the Excel file and write it down in the perfect place in the .txt. For this example that I've write, it would be..

    Please Login or Register  to view this content.
    I don't know if I could express myself in the better way. If not please say so.

    Thank you so much for anyone that tries to help me!

    Regards

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: VBA: Read cell number and write it in correct place in a .txt

    I suggest on a second sheet, create a formula that produces a single line of output you want. You will need to concatonate your text with the values. A simplistic example would be

    Please Login or Register  to view this content.
    You will need to use chr(34) to produce your double quotes.

    Once you have the second page as you want it, save the book, turn on macro recorder, and then save the page as type *.txt. Turn off the macro recorder and edit the code as needed.
    My approach to providing help is to help you to help yourself. So my answers won't always solve your problem, but hopefully you can learn enough to solve the problem yourself and be more self sufficient for the experience.

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: VBA: Read cell number and write it in correct place in a .txt

    Dear Mallycat,

    Thanks for your answer. Sorry but I did not understand how to do what you have explained. Isn't there a way to to what I want with a macro? I will post here a code that I'm using to add text to a .txt a little bit similar with this, but that I don't know how to adapt it to this problem:

    Please Login or Register  to view this content.
    Is there a way to use this kind of code to solve my current problem?

    Thanks

  4. #4
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: VBA: Read cell number and write it in correct place in a .txt

    There is always a way with VBA. Try this. Run it from your open sheet with the input data you describe.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: VBA: Read cell number and write it in correct place in a .txt

    Dear Mallycat,

    Now we have some results :D But there are 2 problems:

    1 - With this code all .txt lines are like this:
    Please Login or Register  to view this content.
    This nm '4 should not be changed from the originals.

    2 - The .txt file that exists has more text than this lines. With this code, the .txt is completely erased and only writes the data we want. Is there any way to maintain the original text and not to erase it all over?

    Thank you so much, and sorry for all the work..

  6. #6
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: VBA: Read cell number and write it in correct place in a .txt

    if you want to append data to an existing file, just change the second line of code from "For Output" to "For Append".

    Regarding nm '4, where does this value come from? it is not the same as sacr1 and it doesn't seem to start from 1.

  7. #7
    Forum Contributor
    Join Date
    05-09-2013
    Location
    london
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: VBA: Read cell number and write it in correct place in a .txt

    Dear Mallycat,

    With the "For Append" option, the code does not change my .txt but just add the lines to the .txt

    Here I attach one part of the .txt that I want to be changed. I would like that automatically the VBA code changed the "Area_here" with the Area read in the Excel file. But as you can see, in the top of the .txt there are some text lines that I do not want to be deleted.

    Regarding the nm'4 its a "code name". Each line has one of this "code name" and does not start from 1. Just imagine that is a random text number.

    Thank you so much

    test.txt

  8. #8
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: VBA: Read cell number and write it in correct place in a .txt

    What you are asking for is changing each time you post. Now I see there are 3 lines at the top of the txt that are to be ignored, plus now the " are missing from the input file. It is difficult to help you without all the information. If you post an exact set of sample data spreadsheet and txt file, and an exact set of output, I can create the macro for you.

+ 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