ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need help with For-Next block (https://www.excelbanter.com/excel-programming/398090-need-help-next-block.html)

Bob

Need help with For-Next block
 
I have the following code block:

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Next x
End If
.... more instructions ...
Next x

but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.





Mike H

Need help with For-Next block
 
Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.





JW[_2_]

Need help with For-Next block
 
x will automatically increment wen the Next x line is reached. You do
not need two Next x lines to pull this off.
Do you want to skip over the ...more instructions... portion if the If
statement is true? That's the way I am interpreting it. If so, just
throw in an Else clause and remove the unneeded Next x statement.
For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Else
... more instructions ...
End If
Next x

Bob wrote:
I have the following code block:

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Next x
End If
... more instructions ...
Next x

but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.



Bob

Need help with For-Next block
 
Mike,
I should have been more clear in my objective. My apologies.

If the IF-block evaluates to True, not only do I need to increment "x" by 1,
but I also need to skip the "... more instructions ..." section, too. That's
the part I'm struggling with.

Thanks again for the help.
Bob


"Mike H" wrote:

Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.





Mike H

Need help with For-Next block
 
Bob,

There will probably be cries of outrage and spaghetti code accusations but
I'd use Goto like this

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Goto 100
End If
.... more instructions ...
100
Next x


X will then increment by 1 when it reaches Next X

Mike

"Bob" wrote:

Mike,
I should have been more clear in my objective. My apologies.

If the IF-block evaluates to True, not only do I need to increment "x" by 1,
but I also need to skip the "... more instructions ..." section, too. That's
the part I'm struggling with.

Thanks again for the help.
Bob


"Mike H" wrote:

Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.





JW[_2_]

Need help with For-Next block
 
Not to take anything away from Mike's code, but I still believe that a
simple Else clause is all you need.
Do this if true
Else
Do this if false
Next x

Seems to me to be exactly what you want. See the code I posted
earlier. If I am completely lost, then please set me straight.

Bob wrote:
Mike,
I should have been more clear in my objective. My apologies.

If the IF-block evaluates to True, not only do I need to increment "x" by 1,
but I also need to skip the "... more instructions ..." section, too. That's
the part I'm struggling with.

Thanks again for the help.
Bob


"Mike H" wrote:

Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.






Bob

Need help with For-Next block
 
JW,
Yes, I also wanted to skip over the ...more instructions... portion.
Inserting an Else clause did the trick. (I'm embarrassed that I didn't think
of that.) Thanks for your help!
Bob


"JW" wrote:

x will automatically increment wen the Next x line is reached. You do
not need two Next x lines to pull this off.
Do you want to skip over the ...more instructions... portion if the If
statement is true? That's the way I am interpreting it. If so, just
throw in an Else clause and remove the unneeded Next x statement.
For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Else
... more instructions ...
End If
Next x

Bob wrote:
I have the following code block:

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Next x
End If
... more instructions ...
Next x

but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.




Mike H

Need help with For-Next block
 
JW,

I agree your solution is much neater.

Mike

"JW" wrote:

Not to take anything away from Mike's code, but I still believe that a
simple Else clause is all you need.
Do this if true
Else
Do this if false
Next x

Seems to me to be exactly what you want. See the code I posted
earlier. If I am completely lost, then please set me straight.

Bob wrote:
Mike,
I should have been more clear in my objective. My apologies.

If the IF-block evaluates to True, not only do I need to increment "x" by 1,
but I also need to skip the "... more instructions ..." section, too. That's
the part I'm struggling with.

Thanks again for the help.
Bob


"Mike H" wrote:

Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.







Bob

Need help with For-Next block
 
Mike,
I, too, prefer NOT to use Goto. JW suggested using an Else clause which did
the trick. Thanks all the same.
Bob


"Mike H" wrote:

Bob,

There will probably be cries of outrage and spaghetti code accusations but
I'd use Goto like this

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Goto 100
End If
... more instructions ...
100
Next x


X will then increment by 1 when it reaches Next X

Mike

"Bob" wrote:

Mike,
I should have been more clear in my objective. My apologies.

If the IF-block evaluates to True, not only do I need to increment "x" by 1,
but I also need to skip the "... more instructions ..." section, too. That's
the part I'm struggling with.

Thanks again for the help.
Bob


"Mike H" wrote:

Bob,

Try

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x=x+1
End If
... more instructions ...
Next x

Mike


"Bob" wrote:

I have the following code block:


but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.





Nobody[_4_]

Need help with For-Next block
 
Perhaps...

For x = 1 to 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
x = x +1
Else
... more instructions ...
End If
Next x

"Bob" wrote in message
...
I have the following code block:

For x = 1 To 10
If RowIsBlank(8) = True Then
Application.DisplayAlerts = False
Workbooks(TargetFile(x)).Close
Application.DisplayAlerts = True
Next x
End If
... more instructions ...
Next x

but when I run the code, I receive the expected "Next without For" error
message.
I need to somehow increment "x" when the IF-block evaluates to True. Can
someone kindly tell me how to pull this off?

Thanks in advance for any help.








All times are GMT +1. The time now is 01:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com