VBA IF QUESTION

  • Thread starter Thread starter Hamed parhizkar
  • Start date Start date
H

Hamed parhizkar

I have this formula in vba:

If Range("C28") > 0 Then Range("A23:D34").Select
ActiveSheet.PageSetup.PrintArea = "$A$23:$D$34"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

It works fine and prints but it prints even if the cell is zero. What am I
doing wrong. I only need it to print the range if c28 is greater than zero.

Please help.
 
when you have code on the same line as the THEN it is the only part of the
code that is checked



If Range("C28") > 0 Then _
Range("A23:D34").Select
ActiveSheet.PageSetup.PrintArea = "$A$23:$D$34"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
end if
 
Don't put the continuation line after the then. I caught the mistake just
when I press the reply button as the window was closing.

from
then _
to
then
 
Change to following:-


If Range("C28") > 0 Then
Range("A23:D34").Select
ActiveSheet.PageSetup.PrintArea = "$A$23:$D$34"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
end if
 
So what is the formula suppose to look like, I dont have a _ in there?

If Range("C28") > 0 Then Range("A23:D34").Select
 
Sub printif()
If Range("c28") > 0 Then Range("a23:d34").Printout 'Preview
End Sub
 
It should look like this...

If Range("C28") > 0 Then
Range("A23:D34").Select
ActiveSheet.PageSetup.PrintArea = "$A$23:$D$34"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If

Whenever you have multiple statements that, as a group, will only be
executed for a given logical condition, you put all the multiple statements
between an If-Then statement (with nothing following the Then) and an End If
statement.

Rick
 
Back
Top