Using if/then/else block to end repeat block

Hey guys,

I've been grappling with AppleScript for 3 weeks now.

I've got a chunk of the following code to work but it just won't compile.
Working with Excel 2004 here.

What I'm trying to do is to use an if/then/else block to break out of a repeat block.
After the autofiltering, if visibleCount is 0, I want it to move on to the next item in PnList.
But if visibleCount is not 0, I want it to carry on with the code.

I'd appreciate any help I can get.

Cheers,
Wilfred

the code looks like this:


repeat with p from 1 to (count of items in PnList)
tell range "A2" of active sheet
autofilter range
autofilter range field 1 criteria1 ¬
(item p of PnList)
end tell

set visibleCount to "A1"
set value of visibleCount to "=SUBTOTAL(2,A:A)-2"

if visibleCount = 0

then end repeat

else

set myRange2 to (special cells (range ("3:" & (count rows in used range of active sheet) as string)) type cell type visible)

set RStart2 to first row index of myRange2
set REnd2 to count rows in myRange2
set REnd2 to RStart2 + REnd2 - 1
show all data of active sheet
copy range range (RStart2 & ":" & REnd2 as string)

--new sheet with only NPI data
set NewBk2 to make new workbook
set RRNPI to get name of active workbook

paste special range "A3" what values


end repeat

Message was edited by: wilfredph

MacBook Pro, Mac OS X (10.6.4)

Posted on Jul 26, 2010 12:24 AM

Reply
7 replies

Jul 26, 2010 12:39 AM in response to Niel

Ah okay but what do I replace?
I get a Expected end of line, etc. but found “repeat”. error message when I do the following.

if visibleCount = 0 then
exit repeat

else

set myRange2 to (special cells (range ("3:" & (count rows in used range of active sheet) as string)) type cell type visible)

set RStart2 to first row index of myRange2
set REnd2 to count rows in myRange2
set REnd2 to RStart2 + REnd2 - 1
show all data of active sheet
copy range range (RStart2 & ":" & REnd2 as string)

--new sheet with only NPI data
set NewBk2 to make new workbook
set RRNPI to get name of active workbook

paste special range "A3" what values


end repeat

Jul 27, 2010 6:38 AM in response to wilfredph

Hello

If I understand you correctly, what you're looking for would be some construct as listed below. It is equivalent to 'continue' statement in some other programming language, which AppleScript doesn't have. This 'exit repeat' statement in conjunction with the inner one-time repeat loop realises flow control equivalent to 'continue' statement for outer loop.


set rr to {}
repeat with i from 1 to 10
repeat 1 times
set i to i * (i + 1)
if i mod 6 = 0 then exit repeat -- exit inner loop; continue outer loop
set end of rr to i
end repeat
end repeat
return rr -- {2, 20, 56, 110}


Hope this helps,
H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using if/then/else block to end repeat block

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.