Do While in a For Each Loop

Hi guys,

I’m trying to automate a process that types in a variable from a for each row with a do while condition where the row count doesn’t exceed 2.

strVal = row(“Column1”).ToString

row_Count = DTOut.Rows.Count

condition: row_Count <= 2

My process doesn’t stop typing at the 2nd strVal, it iterates through the whole row. Please assist

As long as you do not alter the row_count value in the do while loop your do while loop will run forever.

Maybe you need a IF statement for your row_count

1 Like

Hi @PeterK thanks for your response, i tried the suggested approach and The Type into activity doesn’t stop at the 2nd StrVal which is the 2nd row of the DT

the do while is not on the correct position. you have to put all of your row processing into the loop.

I think there is a bit of misunderstanding on what the Do While activity actually does.

Do While is a type of loop. It repeats everything in the “Do” section as long as the condition in the “While” Section is true.

It seems like you want to use an “If” activity instead. This activity performs specific actions based on whether the “if” portion of the activity is true or false. So I’m guessing you should replace the Do While activity with an if Activity. In the condition, put row_Count <= 2. On the true side, but your message box activity. Leave the false side blank.

Hi @Dave i tried the suggested approach but the message box with strVal didn’t pop up

Hi @PeterK from the read range, the iteration of strVal output doesn’t stop at Column1 cell 2 it gives an output till it reaches 9 then stops

image

Now that I can see your whole for each row activity, the reason why it isn’t working is because row_Count is never changing value.

You are counting the total number of rows in datatable DT. The number of rows is the same every time, so if it is true once it is always true. If it is false once it is always false. Do you want it to only show the message box if the current row it’s working on is <= 2? If so, remove row_count variable all together. In your if activity change the condition so it is: DT.Rows.IndexOf(row) <= 2

1 Like

it works! will this yield the same results in a Do While? I’m assuming to go through the whole row i’ll have to use DT.Rows.IndexOf(row) < > DT.Rows.IndexOf(row)

No I think there is still a misunderstanding of what the Do While loop is performing. Do While is a loop structure that will perform everything within the “Do” section forever until the “While” condition is not true. I don’t think you’d want to use a Do While here at all.

What the solution is currently doing is going through each DataRow (variable called ‘row’ in your workflow) in your DataTable (variable called ‘DT’ in your workflow). It is then checking the index of the current datarow within the datatable. One important note is that the datatable starts at an index of 0, so if you want only the first 2 rows in the datatable, then your if condition should be <= 1 :slight_smile:

If by “go through the whole row” you mean each column within the row, then you should just add more assign activities. So create a string variable and assign it like this: YourStringVariable = row.item("YourColumnName").ToString

1 Like

Hi @Dave Meant all rows in the DT

You would want to continue using the For Each Row activity instead of the Do While activity. That ensures it goes through each row in the datatable and ends the loop once it is done with the last row.

If you really want to use a Do While (or While) loop, then you certainly can. You just need to create a separate integer variable to keep track of the row index, then increment that index by 1 every loop - i’ll call this integer variable counter.

Assign Counter = 0

All of the below is contained in a the DO section of a Do While activity:

Assign strVal = Dt.Rows(counter).item("Column1").ToString
msgbox strVal
Assign counter = counter + 1

In the Condition section of the Do While activity, put the following:

DT.Rows.IndexOf(Dt.Rows(0)) <= 2

It is better to just use a break activity in your for each row loop if you only want to loop through until the condition is reached, as using the Do While loop is a bit more complicated.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.