Next w/o For If/Then Loop

  • Thread starter Thread starter littlegreenmen1
  • Start date Start date
L

littlegreenmen1

I have a For/Next Loop setup, but in the middle of it I need an If/Then
Statement. My For/Next Loop scans a list of Dates and then transfers
them to an Outlook Calender. However, I dont need it to transfer
anything from before the day I run the macro. I tried inserting the
following code:

If olDate < Date Then
Next x
End If

When I use this I get a Next without For error. Is there a way to use
Next inside an If/Then conditional if the For resides outside the
If/Then? Thank you very much for any and all help.
 
It will be interesting to see others' solutions, but I think, instead of
what you've got, I'd do:

If oldate>=Date
'make calender code'
end if
next x
 
No, the code syntax you describe will not work. Try something
like

For X = whatever to whatever
If olDate >= Date Then
' your code here
End If
Next X

You could also forego using a For Next loop and use a Do
While/Until Loop, which allows you to change an index whenever
you need to.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"littlegreenmen1"
<[email protected]>
wrote in message
news:[email protected]...
 
This is a bit of heresy.

Use responsibly, or don't use at all.

For cust = 1 to 10000
If cust.TestValue1 = False Then GoTo NextCustomer
If cust.TestValue2 = True Then GoTo NextCustomer
......
If cust.TestValue10 = 3 Or DayOfWeek = "Jaborwocky" Then GoTo
NextCustomer

' Code to process any x's that got this far
................
NextCustomer:
Next cust

Using Labels for "jump-to"s is generally very frowned upon these days
outside of Error Handling, because it leads to sloppy practices and
spaghetti code. No argument from me. However, I personally make this one
exception frequently, as a substitute for the "GoTo Next" statement that
doesn't exist (but should). I limit it's use to when I have umpteen
evaluations to apply to an item before it "qualifies" for further action.
Yes, the code could be structured differently but I find this *much* easier
to develop, debug, read & maintain. New test? just add a line, no need to
rebuild endlessly nested If statements. The "jump" is confined to the end
of the Loop, so no harm done in my mind since it as "self contained" as the
built-in "Exit For".

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"littlegreenmen1"
 
George,

I agree. A few short jumps don't hurt no one no how. Ask an assembler
programmer. I, when feeling very civic-minded and responsible, use
something like:

For cust = 1 to 10000
Do
If cust.TestValue1 = False Then Exit Do
If cust.TestValue2 = True Then Exit Do
......
If cust.TestValue10 = 3 Or DayOfWeek = "Jaborwocky" Then Exit DO

' Code to process any x's that got this far
................
Loop While "pigs" = "fly"
Next cust
 
*Very* interesting. I'll keep that structure in mind.

However, I'm not 100% sure about the civic-mindedness aspect: I can easily
see it blowing the mental fuse of a neophyte coming across it during
mainenance.

:-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top