Do Until EOF Does Not Loop Properly | MrExcel Message Board

MrExcel Message Board
  • Forums New posts Search forums Board Rules
  • What's new Featured content New posts New Excel articles Latest activity
  • New posts
  • Excel Articles Latest reviews Search Excel articles
  • MrExcel Publishing MrExcel Homepage MrExcel Bookstore MrExcel Seminars Excel Consulting Services
Log in Register What's new Search

Search

Everywhere Threads This forum This thread Search titles only Note By: Search Advanced search…
  • New posts
  • Search forums
  • Board Rules
Menu Log in Register Install the app Install How to install the app on iOS

Follow along with the video below to see how to install our site as a web app on your home screen.

Note: This feature may not be available in some browsers.

  • If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.
  • Forums
  • Question Forums
  • Excel Questions
You are using an out of date browser. It may not display this or other websites correctly.You should upgrade or use an alternative browser. Do Until EOF does not loop properly
  • Thread starter Thread starter mtenorio
  • Start date Start date Jan 12, 2015
  • Tags Tags eof text file vba
M

mtenorio

New Member
Joined Sep 5, 2014 Messages 18 Hi guys! I am working on an application where a text file is opened then some lines are deleted based on a criteria. I am using the following code to read the text per line: Code: If FileName(root_loc, period, wsc_code) = "" Then MsgBox "There is no e-file found for the period M" & period & "." Else 'Opens the e-file Open FileName(root_loc, period, wsc_code) For Input As #1 row_number = 0 Do Until EOF(1) 'reads the text line per line until the end of file Line Input #1, LineFromFile row_number = row_number + 1 Loop Close #1 Debug.Print row_number End If Supposedly I would get 58 for the Debug.Print row_number since 58 is the number of lines on the text file, but instead I get 1. But when I try Debug.Print LineFromFile, the whole file gets printed.. Do you guys have an idea why I am getting 1 only for the number of lines? Thanks a lot!

Excel Facts

Is there a shortcut key for strikethrough? Click here to reveal answer Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5. Sort by date Sort by votes D

Domenic

MrExcel MVP
Joined Mar 10, 2004 Messages 21,801 Office Version
  1. 365
Platform
  1. Windows
That's probably because a line feed character (vbLf) is being used as an end of line marker, instead of a carriage return (vbCr) or carriage return and line feed (vbCrLf). One alternative would be to use the FileSystemObject. Otherwise, the code could be amended as follows... Code: [COLOR=darkblue]Dim[/COLOR] my_string [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] [COLOR=darkblue]Dim[/COLOR] my_lines [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR] [COLOR=darkblue]Dim[/COLOR] LineFromFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] [COLOR=darkblue]Dim[/COLOR] row_number [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR] [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR] [COLOR=green]'[/COLOR] ' ' [COLOR=green]'[/COLOR] [COLOR=darkblue]Open[/COLOR] Filename(root_loc, Period, wsc_code) [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Input[/COLOR] [COLOR=darkblue]As[/COLOR] #1 my_string = [COLOR=darkblue]Input[/COLOR](LOF(1), #1) [COLOR=darkblue]Close[/COLOR] #1 my_lines = Split(my_string, vbLf) row_number = 0 [COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](my_lines) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](my_lines) LineFromFile = my_lines(i) row_number = row_number + 1 [COLOR=darkblue]Next[/COLOR] i Debug.Print row_number [COLOR=green]'[/COLOR] ' ' [COLOR=green]'[/COLOR] Hope this helps! Upvote 0 M

mtenorio

New Member
Joined Sep 5, 2014 Messages 18 Hello Domenic! I've tried this and it worked perfectly fine! thanks a lot!!!:) Upvote 0 D

Domenic

MrExcel MVP
Joined Mar 10, 2004 Messages 21,801 Office Version
  1. 365
Platform
  1. Windows
You're very welcome! Upvote 0 I

Ileska

New Member
Joined Sep 15, 2022 Messages 1 Office Version
  1. 365
Platform
  1. Windows
That was super helpful, thanks a lot Domenic!!! Upvote 0 You must log in or register to reply here.

Similar threads

TryingToLearn
  • Question Question
Open a csv file but don't import all data and don't insert ""
  • TryingToLearn
  • Oct 5, 2024
  • Excel Questions
Replies 1 Views 162 Oct 5, 2024 DanteAmor DanteAmor R
  • Question Question
Add a code or number at the end of the file name using VBA (Looped)
  • rdtoner
  • Dec 4, 2024
  • Excel Questions
Replies 3 Views 176 Dec 4, 2024 HighAndWilder H B
  • Solved
VBA to read 1 line at a time from 2 CSVs and write them sequentially to the active workbook
  • BakerSteve
  • May 6, 2024
  • Excel Questions
Replies 8 Views 490 May 8, 2024 BakerSteve B O
  • Question Question
File name does not recalculate
  • ozbeachbum
  • Oct 30, 2024
  • Excel Questions
Replies 5 Views 105 Oct 30, 2024 ozbeachbum O C
  • Question Question
VBA: open and copy data from multiple (txt. files) to excel
  • CRG
  • Mar 15, 2024
  • Excel Questions
Replies 1 Views 634 Mar 15, 2024 NdNoviceHlp NdNoviceHlp Share: Facebook X (Twitter) LinkedIn Reddit Tumblr WhatsApp Email Share Link

Forum statistics

Threads 1,225,145 Messages 6,183,146 Members 453,148 Latest member yevhen

Share this page

Facebook X (Twitter) LinkedIn Reddit Tumblr WhatsApp Email Share Link
  • Forums
  • Question Forums
  • Excel Questions

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock1)Click on the icon in the browser’s toolbar.2)Click on the icon in the browser’s toolbar.2)Click on the "Pause on this site" option. Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus1)Click on the icon in the browser’s toolbar.2)Click on the toggle to disable it for "mrexcel.com". Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin1)Click on the icon in the browser’s toolbar.2)Click on the "Power" button.3)Click on the "Refresh" button. Go back

Disable uBlock

Follow these easy steps to disable uBlock1)Click on the icon in the browser’s toolbar.2)Click on the "Power" button.3)Click on the "Refresh" button. Go back Continue without adsI've disabled my adblock Back Top

Từ khóa » Visual Basic Do Until Eof