Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default Pls help when "for each" is empty

I have the following VB macro fragment:

for each cell in Selection
for each p in cell.Precedents
debug.print "p=" & typename(p)
next p
next cell

When the selected cell has =1+2, for example, "for each p" fails with
the error "no cells were found" because "p" is Empty.

I remedy the error by adding the statement "on error resume next".

However, the debug.print statement is still executed(!). I expected
execution to go to the "next p" statement and fall out of the loop
because "p" is Empty.

I tried to avoid the problem by adding the statement "if IsEmpty(p)
then exit for" after "for each p".

But the debug.print statement is __still__ executed (!). The "exit
for" statement is executed, but apparently it is treated as an
error(!), and presumably the "on error resume next" statement takes
effect.

I tried to avoid __that__ problem by adding the statement "on error
goto 0" before "exit for", in addition to other required syntax
changes (see endnotes).

But then I get the error "for loop not initialized".

The only thing that seems to work is "if IsEmpty(p) then goto done".

But I would prefer something like "exit for" for aesthetic reasons,
especially when I have several nested for-loops that might encounter
this condition (empty "for each"), which would require labels like
"done1", "done2", etc or other distinctive labels.

Am I missing something? Or is a "goto" the only way to abort an empty
"for each" loop?


-----

Endnotes

1. Preferred solution:

on error resume next
for each cell in Selection
for each p in cell.Precedents
if IsEmpty(p) then exit for
debug.print "p=" & typename(p)
next p
next cell


2. Failed solution:

for each cell in Selection
on error resume next
for each p in cell.Precedents
if IsEmpty(p) then
on error goto 0
exit for
end if
debug.print "p=" & typename(p)
next p
next cell


3. Undesirable working solution:

on error resume next
for each cell in Selection
for each p in cell.Precedents
if IsEmpty(p) then goto Done
debug.print "p=" & typename(p)
next p
Done:
next cell

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Pls help when "for each" is empty

Hi Joeu,

Try something like:

'=============
Public Sub Tester()
Dim rCell As Range
Dim rngPrecedents As Range
Dim pCell As Range

For Each rCell In Selection
On Error Resume Next
Set rngPrecedents = rCell.Precedents
On Error GoTo 0
If Not rngPrecedents Is Nothing Then
For Each pCell In rCell.Precedents
Debug.Print "p=" & TypeName(pCell)
Next pCell
End If
Next rCell
On Error GoTo 0
End Sub
'<<=============

However, will the precedents not always return
'Range'?



---
Regards,
Norman



"joeu2004" wrote in message
ups.com...
I have the following VB macro fragment:

for each cell in Selection
for each p in cell.Precedents
debug.print "p=" & typename(p)
next p
next cell

When the selected cell has =1+2, for example, "for each p" fails with
the error "no cells were found" because "p" is Empty.

I remedy the error by adding the statement "on error resume next".

However, the debug.print statement is still executed(!). I expected
execution to go to the "next p" statement and fall out of the loop
because "p" is Empty.

I tried to avoid the problem by adding the statement "if IsEmpty(p)
then exit for" after "for each p".

But the debug.print statement is __still__ executed (!). The "exit
for" statement is executed, but apparently it is treated as an
error(!), and presumably the "on error resume next" statement takes
effect.

I tried to avoid __that__ problem by adding the statement "on error
goto 0" before "exit for", in addition to other required syntax
changes (see endnotes).

But then I get the error "for loop not initialized".

The only thing that seems to work is "if IsEmpty(p) then goto done".

But I would prefer something like "exit for" for aesthetic reasons,
especially when I have several nested for-loops that might encounter
this condition (empty "for each"), which would require labels like
"done1", "done2", etc or other distinctive labels.

Am I missing something? Or is a "goto" the only way to abort an empty
"for each" loop?


-----

Endnotes

1. Preferred solution:

on error resume next
for each cell in Selection
for each p in cell.Precedents
if IsEmpty(p) then exit for
debug.print "p=" & typename(p)
next p
next cell


2. Failed solution:

for each cell in Selection
on error resume next
for each p in cell.Precedents
if IsEmpty(p) then
on error goto 0
exit for
end if
debug.print "p=" & typename(p)
next p
next cell


3. Undesirable working solution:

on error resume next
for each cell in Selection
for each p in cell.Precedents
if IsEmpty(p) then goto Done
debug.print "p=" & typename(p)
next p
Done:
next cell



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Pls help when "for each" is empty

Hi Joeu,

Please delete the penultimate line:

On Error GoTo 0



---
Regards,
Norman


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
change "true" and "false" to "availble" and "out of stock" inthestands Excel Worksheet Functions 2 July 19th 07 07:05 PM
HELP on "left","right","find","len","substitute" functions serene83 Excel Discussion (Misc queries) 5 June 27th 06 02:23 AM
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next BCB New Users to Excel 7 May 13th 06 10:02 PM
If changed array formula reduce ""\""\""\ - signs to #Missing, will it make ... Maria J-son[_2_] Excel Programming 2 March 5th 06 12:20 PM


All times are GMT +1. The time now is 11:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"