VBA Loops with If,Then and Else Commands

pinkpanther6666

Board Regular
Joined
Feb 13, 2008
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Good Evening All,

Im trying to write some vba where i have to generate up to 30 reports from my spreadsheet
But sometimes the value in Cell C5 is 0 and i dont want a report generating for that
the SaveReport Macro basically saves the report as a pdf
My VBA Code is as follows

For A = 1 To 30
If Range("C5").Value >= 1 Then Call SaveReport
Else Next A


What im trying to write is if the Value of C5 is Greater and Equal to 1 then call SaveReport and then Next A


Hope anyone can help


Many Thanks


Steve
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
My VBA Code is as follows

For A = 1 To 30
If Range("C5").Value >= 1 Then Call SaveReport
Else Next A

What im trying to write is if the Value of C5 is Greater and Equal to 1 then call SaveReport and then Next A
Write that code this way...
Code:
For A = 1 To 30
  If Range("C5").Value >- 1 Then Call SaveReport
Next
 
Upvote 0
Sounds like you don't need Else part.
Code:
For A = 1 To 30
	If Range("C5").Value >= 1 Then 
		Call SaveReport
	End If
Next A
 
Upvote 0
Write that code this way...
Code:
For A = 1 To 30
  If Range("C5").Value >- 1 Then Call SaveReport
Next
Sounds like you don't need Else part.
Code:
For A = 1 To 30
	If Range("C5").Value >= 1 Then 
		Call SaveReport
	End If
Next A
Just to clarify the difference between the above two codes... there is none except for the format.

For the first code
-----------------------------------
This is the single line form of the If..Then statement. When you only have a single statement to execute when the If condition is True, you can put it on the same line as the If..Then and you do not need an End If to close it off.


For the second code
-----------------------------------
This is the block form of the If..Then statement. When you have more than one statements to execute when the If condition is True, you place them each on their own line underneath the If..Then statement and above an End If statement. You can also use the block form of the If..Then statement for a single line of code to execute when the If condition is True if you want... you have a choice of formats to use (single line or block form) when you only have a single line of code.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top