[Solved] Repeat until not equal

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
cnfarber
Newbie
Posts: 5
Joined: Thu Aug 02, 2018 7:38 pm

[Solved] Repeat until not equal

Post by cnfarber » Thu Aug 02, 2018 7:53 pm

I'm having issues figuring out the correct syntax

I am looking to repeat a function until the row after the row I am working on is a different value. These values are coming from excel

Value 1: XLGetCell>xlH,Sheet1,row,2,field_2
Value 2: XLGetCell>xlH,Sheet1,check,2,field_22

I am having issues with the syntax of comparing field_2 to field_22
I have tried a few variations similar to:
Until>%field_2% != %field_22%

The error I get is:
Repeat> %field_2 ! Not Found!


I also want to add 1 to a count called "Lines" each time the function will repeat

if>%field_2%=%field_22%
Let>Lines=Lines+1
endif

Not sure if this is working either
Last edited by cnfarber on Mon Aug 06, 2018 3:35 pm, edited 1 time in total.

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Repeat until not equal

Post by Marcus Tettmar » Thu Aug 02, 2018 9:16 pm

Not equals should be <>, so:

Until>field_2<>field_22

Let>Lines=Lines+1 is correct. But make sure you initialise Lines, to e.g. 0 before the loop.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

cnfarber
Newbie
Posts: 5
Joined: Thu Aug 02, 2018 7:38 pm

Re: Repeat until not equal

Post by cnfarber » Fri Aug 03, 2018 8:28 pm

I'm not getting an error message but it isn't repeating

Here is my entire test:

Let>filename=C:\Users\USER\Desktop\ExcelTest.xlsx
XLOpen>filename,1,xlH
setfocus>ExcelTest
let>row=1
let>check=2

repeat
Let>row=row+1
Let>check=check+1
XLGetCell>xlH,Sheet1,row,2,field_2
XLGetCell>xlH,Sheet1,check,2,field_22
send>%field_22%
press>tab
send>%field_2%
press>tab
wait>2
Until>%field_2<>field_22%


It will send the same value for both fields, which means it should repeat. After running through once, it stops.

I made sure that the the value it is using in excel is a number

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Repeat until not equal

Post by Grovkillen » Sat Aug 04, 2018 12:58 pm

Have you studied the manual? The repeat syntax is not even close to be correct.
Let>ME=%Script%

Running: 15.0.24
version history

cnfarber
Newbie
Posts: 5
Joined: Thu Aug 02, 2018 7:38 pm

Re: [Solved] Repeat until not equal

Post by cnfarber » Mon Aug 06, 2018 3:39 pm

If anyone runs into something similar to this, here was my solution.
Added:

Let>stop=0
Repeat>row

if>field_2<>field_22
let>stop=row
end if

Until>row=stop


Special shout out to Grovkillen for being so helpful!

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: [Solved] Repeat until not equal

Post by Marcus Tettmar » Tue Aug 07, 2018 9:52 am

Could also have done:

Code: Select all

Let>stop=0
While>stop=0
  if>field_2<>field_22
    let>stop=row
  end if
EndWhile
Or you could remove the If and have the field2<>field22 comparison as part of the while condition:

Code: Select all

Let>field_2=1
Let>field_22=2
While>field_2<>field_22
  //bla bla bla
EndWhile
But also I would probably have stored the last value in a variable rather than XLGetCell the next row each time. Only really need to get one row, and simply store the last value at the start of the loop. Your way you're reading the same row twice. No need.

Of course you could also just use a Label and Goto.

There's a tonne of ways to do it. It's just simple looping:
https://help.mjtnet.com/article/144-how ... p-the-loop
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts