+ Reply to Thread
Results 1 to 17 of 17

Reading line by line data from Text file

  1. #1
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Reading line by line data from Text file

    Dear all,

    Thank you very much for having this forums and help.

    I am trying to create a spreadsheet which starts with a user form of which i can input a number into a textbox. On clicking a button in the user form, this number will correspond to a certain row number within a separate text file, select and copy that entire row and print it on excel.

    I am very new to VBA and would appreciate if someone could give me a hand with this, i have attached the excel file for reference. thanks in advance!



    My current code (which doesn't work) to copy off the spreadsheet is something like this:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by wyldencl; 05-14-2015 at 03:10 AM. Reason: Added Code tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    Welcome to the Forum!

    You should also post a sample text file that can be used for testing.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    Thx for your reply. I have uploaded the txt file.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    Thanks for attaching the text file. I have a few questions though.

    1) How are the fields (columns) separated?
    Generally a text file uses either commas or tabs to separate the columns, or sometimes each field is a specific number of spaces wide. In your file it appears to be tabs, but I'm not certain.
    Rows are terminated with either a Line Feed, Carriage Return, or both. I don't see either in your file.

    2) Is this text file download from the web?
    I ask because it is stock data and most sites have a text file download button. The files I have seen in the US have the values separated by commas.

    3) How are the lines numbered in the text file?
    There appear to be no line numbers in the file.

    Knowing how the file is formatted is key to coding a macro to read, find, and write data to and from the file. Attached is a copy of your text file that I have separated with tabs. A header "Empty" has been added to show where the empty column is. Each field is separated by a single tab. That is why the data looks misaligned with the headers. The macro can read it and place the proper value in the proper column of the worksheet.
    Attached Files Attached Files
    Last edited by Leith Ross; 05-14-2015 at 04:22 PM.

  5. #5
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    Thanks so much again for your help!

    I was oblivious of the spacing rule before this. Yes the data can be formatted to look like that in your text file!

    Is there a way for vba to read the lines without proper numbering on the right hand side? If so won't be a problem as well, it can be added.

    How will we write a basic code to reference the text file outside of excel. Thanks again in advance!

    Quote Originally Posted by Leith Ross View Post
    Hello wyldencl,

    Thanks for attaching the text file. I have a few questions though.

    1) How are the fields (columns) separated?
    Generally a text file uses either commas or tabs to separate the columns, or sometimes each field is a specific number of spaces wide. In your file it appears to be tabs, but I'm not certain.
    Rows are terminated with either a Line Feed, Carriage Return, or both. I don't see either in your file.

    2) Is this text file download from the web?
    I ask because it is stock data and most sites have a text file download button. The files I have seen in the US have the values separated by commas.

    3) How are the lines numbered in the text file?
    There appear to be no line numbers in the file.

    Knowing how the file is formatted is key to coding a macro to read, find, and write data to and from the file. Attached is a copy of your text file that I have separated with tabs. A header "Empty" has been added to show where the empty column is. Each field is separated by a single tab. That is why the data looks misaligned with the headers. The macro can read it and place the proper value in the proper column of the worksheet.

  6. #6
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    Thanks so much again for your help!

    I was oblivious of the spacing rule before this. Yes the data can be formatted to look like that in your text file!

    Is there a way for vba to read the lines without proper numbering on the right hand side? If so won't be a problem as well, it can be added.

    How will we write a basic code to reference the text file outside of excel. Thanks again in advance appreciate it!


    Quote Originally Posted by Leith Ross View Post
    Hello wyldencl,

    Thanks for attaching the text file. I have a few questions though.

    1) How are the fields (columns) separated?
    Generally a text file uses either commas or tabs to separate the columns, or sometimes each field is a specific number of spaces wide. In your file it appears to be tabs, but I'm not certain.
    Rows are terminated with either a Line Feed, Carriage Return, or both. I don't see either in your file.

    2) Is this text file download from the web?
    I ask because it is stock data and most sites have a text file download button. The files I have seen in the US have the values separated by commas.

    3) How are the lines numbered in the text file?
    There appear to be no line numbers in the file.

    Knowing how the file is formatted is key to coding a macro to read, find, and write data to and from the file. Attached is a copy of your text file that I have separated with tabs. A header "Empty" has been added to show where the empty column is. Each field is separated by a single tab. That is why the data looks misaligned with the headers. The macro can read it and place the proper value in the proper column of the worksheet.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,


    Yes, VBA can read the file without proper number. However, it is better if line numbers or some other unique record identifier is present.

    From your last post, it sounds like VBA will create this text file from the information on the worksheet and the UserForm will edit the worksheet and update the text file as needed.

    Am I correct about that?

  8. #8
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    VBA does not create the text file. It will be generated from another system. Excel just has to reference that particular file and read off it.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    Is the text file you posted one of these generated files?

  10. #10
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    Yes it is. Will need to read off that file.

  11. #11
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    If possible i can use input on reading off that text file you separated also. Probably could use that as a base to work on.

    I have googled out some codes to output the entire data on excel, but what i need is to specifically use a userform to input a line number and have excel print that particular line on the spreadsheet when i click a button
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 05-17-2015 at 02:42 PM. Reason: Added Code Tags

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    I have made some changes to the workbook you posted. Hopefully this now does what you want.

    There is a button named "Run" on the "Userform" worksheet to display the UserForm. I also included two macros.One to read the raw data from the text file and copy it to Sheet1 (Blad1). This allows the user to select the file to be opened and copied onto Blad1. The other macro uses the input line number from the UserForm and returns the row data associated with it to Blad2 Range("A6").

    New Macros Added
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Leith Ross; 05-17-2015 at 07:38 PM.

  13. #13
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith!

    I cannot be more grateful for your help in this! Works exactly like the way i need it to be. Really fortunate to have people like you around. I hope in time to come i can build up enough skills to recontribute to this forum!

  14. #14
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith!

    I cannot be more grateful for your help in this! Works exactly like the way i need it to be. Really fortunate to have people like you around. I hope in time to come i can build up enough skills to recontribute to this forum!

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    You're welcome. It is good to hear it works like you wanted.

  16. #16
    Registered User
    Join Date
    04-04-2015
    Location
    Newcastle, UK
    MS-Off Ver
    2014
    Posts
    15

    Re: Reading line by line data from Text file

    Hi Leith,

    I am just wondering, is there any way we can skip copying the raw data from text file into "BLAD1" and read straight off it from excel?

  17. #17
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Reading line by line data from Text file

    Hello wyldencl,

    It is much more efficient and faster to work with to copy the text to worksheet. If you are concerned about the users altering the data once it is copied, you can always hide the worksheet from view.

+ 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. Reading array of data line by line (stock tick data)
    By wyldencl in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2015, 03:12 PM
  2. Replies: 12
    Last Post: 01-14-2014, 01:56 PM
  3. [SOLVED] STart reading text file at line 2
    By Striker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2006, 08:35 AM
  4. Reading a text file line by line-How do I open the text file with VBA
    By stressman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2005, 12:29 AM
  5. Reading a text file line by line
    By Foss in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2005, 12:06 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