If statment error

rspalding

Active Member
Joined
Sep 4, 2009
Messages
282
Office Version
  1. 365
Platform
  1. Windows
I'm trying to add the "IF" statements to change my active sheet range. When doing this I get an error.

Set rng = Nothing

rng = if ActiveSheet.Range("P97").Value = "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$73"
End With
End If
If ActiveSheet.Range("P97").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$99"
End With
End If
If ActiveSheet.Range("P123").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$125"
End With
End If
If ActiveSheet.Range("P149").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$151"
End With
End If
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Does this work?

rng =

If ActiveSheet.Range("P97").Value = "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$73"
End With
End If

Else

If ActiveSheet.Range("P97").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$99"
End With
End If

Else

If ActiveSheet.Range("P123").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$125"
End With
End If

Else

If ActiveSheet.Range("P149").Value > "0" Then
With ActiveSheet.PageSetup
ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$151"
End With
End If
 
Upvote 0
Perhaps

Code:
If ActiveSheet.Range("P97").Value = "0" Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$73"
ElseIf ActiveSheet.Range("P97").Value > "0" Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$99"
ElseIf ActiveSheet.Range("P123").Value > "0" Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$125"
ElseIf ActiveSheet.Range("P149").Value > "0" Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$151"
End If
 
Upvote 0
Replace the whole code with this:

Code:
If ActiveSheet.Range("P97").Value = 0 Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$73"
End If
If ActiveSheet.Range("P97").Value > 0 Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$99"
End If
If ActiveSheet.Range("P123").Value > 0 Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$125"
End If
If ActiveSheet.Range("P149").Value > 0 Then
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AF$151"
End If
 
Upvote 0
Or, maybe...

Code:
    With ActiveSheet.PageSetup
        If Range("P97").Value = "0" Then
            .PrintArea = "$A$1:$AF$73"
        ElseIf Range("P97").Value > "0" Then
            .PrintArea = "$A$1:$AF$99"
        ElseIf Range("P123").Value > "0" Then
            .PrintArea = "$A$1:$AF$125"
        ElseIf Range("P149").Value > "0" Then
            .PrintArea = "$A$1:$AF$151"
        End If
    End With
 
Upvote 0
This is the origional statement I'm trying to add the "If" statement to. It is the set rng that i want the "if" statement added to.

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set rng = Nothing
Set rng = ActiveSheet.Range("A1:AF73")
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
 
Upvote 0
VOG,

This is part of an e-mail program. I need the range to change depending on the data in the file so that it will capture the proper range to e-mail. I hope this clarifies what I'm trying to do.

Robert
 
Upvote 0
So is it?

Code:
If ActiveSheet.Range("P97").Value = "0" Then
    Set Rng = Range("$A$1:$AF$73")
ElseIf ActiveSheet.Range("P97").Value > "0" Then
    Set Rng = Range("$A$1:$AF$99")
ElseIf ActiveSheet.Range("P123").Value > "0" Then
    Set Rng = Range("$A$1:$AF$125")
ElseIf ActiveSheet.Range("P149").Value > "0" Then
    Set Rng = Range("$A$1:$AF$151")
End If
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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