can you put an "if then" in a" for each" loop in a macro?


Posted by RT on October 15, 2001 11:44 AM

if not can you call another macro from within a macro...if so how?
thanks

Posted by Dan on October 15, 2001 11:53 AM

Yes, but I think you'll need to be more specific on what you are trying to accomplish to get a better answer!

Posted by Jonathan on October 15, 2001 12:04 PM

Re: can you put an

Both. You can include an If..Then structure inside a For Each loop, like

Dim Cell As Range

For Each Cell In Selection
If Cell.Value < 0 Then
Cell.Interior.Color = vbMagenta
Else
Cell.Interior.Color = vbGreen
End If
<do other stuff inside For..Each loop
Next Cell


Or you can call another macro from within a macro. So if the macro you want to call is called ColorNegativeCells you just mention it in the calling macro:

<do stuff>
<blah blah blah>
ColorNegativeCells
&LT;do more stuff>
&LT;etc.>

Another equivalent way is to include the Call keyword. Although it's not necessary, it may make the code clearer to someone:

&LT;do stuff>
<blah blah blah>
Call ColorNegativeCells
&LT;do more stuff>
&LT;etc.>

HTH




Posted by Jonathan on October 15, 2001 12:06 PM

Re: can you put an

Both. You can include an If..Then structure inside a For Each loop, like

Dim Cell As Range

For Each Cell In Selection
If Cell.Value &LT; 0 Then
Cell.Interior.Color = vbMagenta
Else
Cell.Interior.Color = vbGreen
End If
--do other stuff inside For..Each loop--
Next Cell


Or you can call another macro from within a macro. So if the macro you want to call is called ColorNegativeCells you just mention it in the calling macro:

--do stuff--
--blah blah blah--
ColorNegativeCells
--do more stuff--
etc.

Another equivalent way is to include the Call keyword. Although it's not necessary, it may make the code clearer to someone:

--do stuff--
--blah blah blah--
Call ColorNegativeCells
--do more stuff--
etc.

HTH