loop only first 5 rows

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
hello all -

Quick question and I hope someone can help.....

I have a windows from that reads from an excel file. Everyting works great except I want to limit the amount of rows that my program can read from.

Example:

The program can read each row of the excel file until it reaches no more lines -

VB.NET:
Dim xls As New Excel.Application
            Dim sheet As Excel.Worksheet

            xls.Workbooks.Open(txtExcel.Text)
            sheet = xls.ActiveWorkbook.Sheets(1)


            Dim row As Integer = 1
                       Do Until sheet.Cells(row, 1) Is Nothing OrElse Len(Trim(sheet.Cells(row, 1).value)) = 0

Then on the bottom I have

VB.NET:
                row += 1


            Loop

I would like for it to only read the first 5 rows instead of going all the way to the end.

Would anyone be able to help?

Thanks in advanced

daveofgv
 
To be honest, I'm surprised that you need to ask this question. You already have the parts in place, e.g. a row number and testing multiple conditions on your Do statement. If you want to stop doing when the row number is greater than 5 then you simply add an extra condition to your Do statement that checks that.
 

   row += 1       

   If row > 5 Then
      Exit Do
   End If

Loop

 

   row += 1       

   If row > 5 Then
      Exit Do
   End If

Loop

Nope. That will work but, given that the purpose of a Do loop is to test a condition and exit if it's True anyway, putting an If statement inside the loop to exit is pointless unless it is part way through the body of the loop.
 
I do agree. I took the easy way out on that answer. I've always followed the strict rule that you can't leave a sub/function without getting to the end of it or your doing it wrong. I guess loops/fors should the be same. "exit" is an easy way out... like GOTO in the old days. Shouldn't need to be used if code is written properly.
 
I do agree. I took the easy way out on that answer. I've always followed the strict rule that you can't leave a sub/function without getting to the end of it or your doing it wrong. I guess loops/fors should the be same. "exit" is an easy way out... like GOTO in the old days. Shouldn't need to be used if code is written properly.
I wouldn't necessarily say that. I've found situations where you might want to check one condition part way through the body of the loop and another at either the beginning or the end. In that case I'd go with at least one Exit statement. The alternative would be to use an If statement part way through the loop that checks the same condition as the loop itself, which I've never been keen on.
 
I agree with you there. Normally I try to make it so I can get through the process without jumping out of it. BUT, sometimes you have to. I still use exits in functions but I will still try my best not to. It is part of the language so it's there for a reason, just would rather code it so I don't need "exit" if I don't.
 
I agree with you there. Normally I try to make it so I can get through the process without jumping out of it. BUT, sometimes you have to. I still use exits in functions but I will still try my best not to. It is part of the language so it's there for a reason, just would rather code it so I don't need "exit" if I don't.
:thumb:

Hmmm... the smiley I wanted doesn't exist so I guess it must have been at another forum. Anyway, you get the idea.
 
Did lots of C language on an HP-UX system. So I'm bring my C coding rules with me to VB. I also used a lot of Visual FoxPro which I loved at the time... but Foxpro is a pain in the *** when it comes to error handling and such. I'm very happy programming in .NET now :)
 
Please tell me if I understood you right:

Given the condition that no more than 5 rows should be imported, and in order to avoid the "Exit" command, would the proper code for this case be like this?

Dim row As Integer = 1
Do Until row>5 or sheet.Cells(row, 1) Is Nothing OrElse Len(Trim(sheet.Cells(row, 1).value)) = 0
            ' commands
      ' commands
      ' commands
      row += 1
Loop 


Thank you very much
 
Thank you all, especially VBobCat. After jmcilhinney first posted I realized that I was missing a condition but didn't know exactly where to place it - I tried the IF...Then statement before I read jmchilhinney say "NOPE". LOL. Then VBobCat posted and I leaned my head down and said "dunz me". I have been playing around with the solution for almost a year and have built a lot of features for it (I made this for work and have a lot of features so we have one application for everything) - this was a slight oversight.

Thanks again and once I figure out how to close a this thread I will :tennis:

daveofgv
 
How about:

For x = 1 to 5
If 'any other conditions here
'do this
Next

I think it would also do it, though "Do Until... Loop" avoids making idle loops after core conditions are no longer true.
I'm not sure of how it could make great difference, though.
I didn't mastered most of unwritten rules of choosing the right way, among all those that seem to accomplish the same thing.
It is not clear to me, for instance, if there are other reasons one should avoid "Exit Do", than the spreading of logical tests along the loop, instead of concentrating all of them in the head of the loop.
As I am not professionally trained as a programmer, there's a concern of mine about this: it seems very easy to get stuck with the complexity and variety of statements and commands, and then to end making up fuzzy rollercoasters where sane notions of logic and algorithm-building would allow shorter and better solutions.
Is there a "chart of principles" that a rookie like me should take a look at?
 
Back
Top