VBA IF QUESTION

  • Thread starter Hamed parhizkar
  • 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.
 
J

Joel

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
 
J

Joel

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
 
G

GerryGerry

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
 
H

Hamed parhizkar

So what is the formula suppose to look like, I dont have a _ in there?

If Range("C28") > 0 Then Range("A23:D34").Select
 
D

Don Guillett

Sub printif()
If Range("c28") > 0 Then Range("a23:d34").Printout 'Preview
End Sub
 
R

Rick Rothstein \(MVP - VB\)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top