Results 1 to 7 of 7

Thread: [RESOLVED] Select string of data and reformat into single cell/txt file

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Resolved [RESOLVED] Select string of data and reformat into single cell/txt file

    I have a few hundred lines of data, each containing a 5 or 6 digit number in column C. I need to use them to search in a different program, but i need to have it in the following format:

    111111 OR 222222 OR 33333 OR 444444 etc.
    How would I go about returning the string of the first 5/6 digits, and put that data either into a single cell in the above format (so I could CTRL+C it), or even a .txt file (doesn't matter to me, as long as it's in that format)

    I tried searching online but there doesn't seem to be a good reference site for VBA. Anyone know of one?
    Last edited by heinz1218; Aug 1st, 2006 at 02:38 PM. Reason: resoloved

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Select string of data and reformat into single cell/txt file

    VB Code:
    1. For i = 1 To Sheet1.UsedRange.Rows.Count
    2.     txtstr = txtstr & " OR " & Cells(i, 3)
    3. Next
    4. somecell = txtstr 'put in cell
    5. f1 = FreeFile
    6. Open "test.txt" For Output As f1
    7. Print #f1, txtstr    '  or write to text file
    8. Close f1

    should do what you ask

    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Re: Select string of data and reformat into single cell/txt file

    Awesome that worked, thanks a bunch.

    what's a "FreeFile", and can you usually get away without Dimming the i and the f1?

    p.s. for anyone reading: it outputs by default to your my documents folder.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Select string of data and reformat into single cell/txt file

    FreeFile is a function that gets the next available reference number for working with files (many people use 1 instead of that, then get errors because they already have a file open).

    You should always Dim your variables (and give them data types too), no matter what they are used for. It makes any code that uses them faster, and it is also more memory efficient.

    p.s. for anyone reading: it outputs by default to your my documents folder.
    Actually it doesnt - that's just a coincidence. If outputs to the folder where your code is being run from (which isnt necessarily the same folder as it is stored in).

    If you want to save in a specific folder, use something like this:
    VB Code:
    1. Open "C:\My Folder\test.txt" For Output As f1
    ..if you want to save it in the same folder as the program, use this:
    VB Code:
    1. Open App.Path & "\test.txt" For Output As f1

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Re: Select string of data and reformat into single cell/txt file

    FreeFile is a function that gets the next available reference number for working with files (many people use 1 instead of that, then get errors because they already have a file open).
    So do you Dim that as something? I'm not quite sure what you mean by "next available reference number"....my bad.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Select string of data and reformat into single cell/txt file

    It's ok, I didn't explain very well!

    You don't need to Dim it, as it is a Function. A function is like the "Close" on the last line of westconn's code, except it returns a value of some sort.


    As to the reference number thing... it is possible to work with several files at the same time, so you need a way of saying which file each line of code should act on - for that you use a reference number.

    Here is an example of code for working with two files (not good code, but explains the concept):
    VB Code:
    1. 'open both files
    2. Open "test1.txt" For Output As #1
    3. Open "test2.txt" For Output As #2
    4.  
    5. 'write some data to them
    6. Print #1, "First file"
    7. Print #2, "Second file"
    8. Print #1, "..a bit more for the first file"
    9.  
    10. 'close both files
    11. Close #1
    12. Close #2
    As you can see, we are using the numbers 1 and 2 to refer to the different files. Using numbers direcltly lke this is a bad idea, as you may have re-used the same number (or accidentally not closed a file), which will give you errors.

    In westconn's code, the variable f1 is used instead, and the value comes from FreeFile - which gives you the next available unused number. For working with two files, it would need to be like this (as the number only gets counted as "used" when you open the file):
    VB Code:
    1. 'open both files
    2. Dim f1 as Integer, f2 as Integer
    3.   f1 = FreeFile
    4.   Open "test1.txt" For Output As f1
    5.   f2 = FreeFile
    6.   Open "test2.txt" For Output As f2
    7. ...

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Re: Select string of data and reformat into single cell/txt file

    Awesome, thanks a bunch man.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width