+ Reply to Thread
Results 1 to 17 of 17

counting names in a text file and writing count in that text file

  1. #1
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    counting names in a text file and writing count in that text file

    Hi can any one help with a VBA macro, I have a folder with text files so N1.txt, N2.txt about 4000 of them in the folder from row 2 in each text file I have data like this written all the way down the text file.

    1,Amazon,1,2,3,4,BUY
    2,EBAY,1,2,4,8,SELL
    3.Amazon,1,0,3,9,BUY
    4,Amazon,1,4,7,4,BUY
    5.EBAY,7,2,9,6,BUY

    In each text file I am trying to do a running count of the the unique names for example using the data above amazon has been written 3 times so next to the first amazon entry I need to write "1" next to the second amazon entry I need to write "2" and next to the third amazon entry I need to write "3"

    EBAY has been written twice so next to the first EBAY entry I need to write "1" and next to the second EBAY entry I need to write "2"

    See last column below

    1,Amazon,1,2,3,4,BUY,1
    2,EBAY,1,2,3,4,SELL,1
    3.Amazon,1,2,3,4,BUY,2
    4,Amazon,1,2,3,4,BUY,3
    5.EBAY,1,2,3,4,BUY,2


    I need to add the comma before writing the count

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: counting names in a text file and writing count in that text file

    Hi Roadhouse,

    You are getting close to needing this formula:

    =COUNTIF(B$1:B1,B1)

    See the attached...
    Countif from the top.xlsx

    BUT - if you want to do lots of files Excel has a newer tool called Get & Transform in 2016 excel that was made for this type of problem.

    http://excelunplugged.com/2015/02/10...n-power-query/

    Time to update your version of Excel?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hi Marvin thanks for your reply but I need this for text files for example N1.txt , N2.txt all the text files in a folder and I really need to use VBA

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: counting names in a text file and writing count in that text file

    OK -
    So you want to pull in 4000 different text files and save 4000 different named .xls files? It would take me a few hours to write the VBA code and then you would need to modify it to point to the correct folder. Are you good with VBA?

    Did I say Power Query will pull in all those text files into a single Excel file. Have you considered a newer version of Excel?

  5. #5
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    No I want to use vba to go through each text file and write the count from row 2 in each row for each text file in the folder, so dont pull in anything just write it to the text files

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: counting names in a text file and writing count in that text file

    OK then,

    You want to write out a text file over the same name, but just with an extra column?

  7. #7
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hi yes I want to access the text files and just add an extra column with the count to each text file so N1.txt we read then write an extra column to this text file and we do this for all the text files in the folder

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: counting names in a text file and writing count in that text file

    Can you attach two sample files and tell what directory they are in so the code will work?

  9. #9
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hi the directory is C:\Users\Admin\Documents\N
    Last edited by Roadhouse; 03-19-2018 at 09:10 AM.

  10. #10
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    here is the two text files they are attached
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Just to clarify most of the text files have a lot more then 20 entries on the sample files I think there are only 20 entries

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: counting names in a text file and writing count in that text file

    Hi,

    Is the running total specific to each file, or should it carry on counting across files?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: counting names in a text file and writing count in that text file

    Assuming count is specific to each file. Something like below.

    Run the code while you have empty sheet active. Change fPath as needed.
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  14. #14
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hello CK76, thank you for the reply I ran the macro and it works thank you for your help
    Last edited by Roadhouse; 03-19-2018 at 12:36 PM.

  15. #15
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,885

    Re: counting names in a text file and writing count in that text file

    It goes in standard module. Have a sheet that's blank active when you run the code.

    Make sure you end fPath with "\" Ex: "C:\Users\Admin\Documents\N\"

    It will read content of each text file in the folder, put values into Excel. Perform calculation on count of each name at last column (AE)
    Load result into array and perform concatenation to put it into original format and output to text.

    This will overwrite original file. So I'd recommend performing test in copy of original files first.

    If you want, add following line below Loop line to let you know that code has finished running.
    Please Login or Register  to view this content.
    With your first sample file result should look like below.
    0.JPG

  16. #16
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hi xlnitwit, the running total is specific to each file so do one file then then reset the count and do the next file

  17. #17
    Forum Contributor
    Join Date
    10-31-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    221

    Re: counting names in a text file and writing count in that text file

    Hi CK76 thankyou for the explanation I had the path file incorrect I needed to add "\" at the end, thank you the macro works fine

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. writing to a text file without quoatations
    By bsapaka in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-06-2014, 01:12 PM
  2. [SOLVED] writing to text file recommendations?
    By nerdzkilla in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2012, 11:10 AM
  3. Writing text to a file
    By fabb2004 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-08-2010, 08:20 PM
  4. Problem with writing to text file from VBA
    By Shane O in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2010, 10:44 AM
  5. Writing results to a text file
    By cameronyoung in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-22-2009, 10:54 AM
  6. Writing number instead of string to text file
    By clayton in forum Excel General
    Replies: 3
    Last Post: 04-13-2008, 10:46 AM
  7. Writing to a text file
    By scantor145 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-30-2005, 04:05 PM

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