Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
littlegreenmen1
 
Posts: n/a
Default Next w/o For If/Then Loop


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.


--
littlegreenmen1
------------------------------------------------------------------------
littlegreenmen1's Profile: http://www.excelforum.com/member.php...o&userid=23978
View this thread: http://www.excelforum.com/showthread...hreadid=377526

  #2   Report Post  
MrShorty
 
Posts: n/a
Default


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


--
MrShorty
------------------------------------------------------------------------
MrShorty's Profile: http://www.excelforum.com/member.php...o&userid=22181
View this thread: http://www.excelforum.com/showthread...hreadid=377526

  #3   Report Post  
Chip Pearson
 
Posts: n/a
Default

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"
<littlegreenmen1.1qbsyd_1118264706.1975@excelfor um-nospam.com
wrote in message
news:littlegreenmen1.1qbsyd_1118264706.1975@excelf orum-nospam.com...

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.


--
littlegreenmen1
------------------------------------------------------------------------
littlegreenmen1's Profile:
http://www.excelforum.com/member.php...o&userid=23978
View this thread:
http://www.excelforum.com/showthread...hreadid=377526



  #4   Report Post  
George Nicholson
 
Posts: n/a
Default

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"
<littlegreenmen1.1qbsyd_1118264706.1975@excelfor um-nospam.com wrote in
message news:littlegreenmen1.1qbsyd_1118264706.1975@excelf orum-nospam.com...

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.


--
littlegreenmen1
------------------------------------------------------------------------
littlegreenmen1's Profile:
http://www.excelforum.com/member.php...o&userid=23978
View this thread: http://www.excelforum.com/showthread...hreadid=377526



  #5   Report Post  
Earl Kiosterud
 
Posts: n/a
Default

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

--
Earl Kiosterud
mvpearl omitthisword at verizon period net
-------------------------------------------

"George Nicholson" wrote in message
...
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"
<littlegreenmen1.1qbsyd_1118264706.1975@excelfor um-nospam.com wrote in
message
news:littlegreenmen1.1qbsyd_1118264706.1975@excelf orum-nospam.com...

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.


--
littlegreenmen1
------------------------------------------------------------------------
littlegreenmen1's Profile:
http://www.excelforum.com/member.php...o&userid=23978
View this thread:
http://www.excelforum.com/showthread...hreadid=377526







  #6   Report Post  
George Nicholson
 
Posts: n/a
Default

*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.

:-)

--
George Nicholson

Remove 'Junk' from return address.


"Earl Kiosterud" wrote in message
...
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

--
Earl Kiosterud
mvpearl omitthisword at verizon period net
-------------------------------------------

"George Nicholson" wrote in message
...
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"
<littlegreenmen1.1qbsyd_1118264706.1975@excelfor um-nospam.com wrote in
message
news:littlegreenmen1.1qbsyd_1118264706.1975@excelf orum-nospam.com...

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.


--
littlegreenmen1
------------------------------------------------------------------------
littlegreenmen1's Profile:
http://www.excelforum.com/member.php...o&userid=23978
View this thread:
http://www.excelforum.com/showthread...hreadid=377526







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
loop trough e-mail address list to send task lists with outlook Paul. Excel Discussion (Misc queries) 2 April 14th 05 11:48 AM
Loop through email address list to send e-mails Paul. Excel Discussion (Misc queries) 1 April 12th 05 12:41 PM
Password Loop question. Andy Tallent Excel Discussion (Misc queries) 1 April 8th 05 01:16 PM
Printe Autofilter Criterias in a Loop Paul. Excel Discussion (Misc queries) 1 March 25th 05 12:51 PM
Please help with loop Mike C Excel Discussion (Misc queries) 3 January 29th 05 02:31 PM


All times are GMT +1. The time now is 03:02 AM.

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

About Us

"It's about Microsoft Excel"