Dynamic print area VBA help

frank850102

New Member
Joined
Jan 26, 2010
Messages
7
Hi all

I have an excel workbook, which has 6 worksheets. For one of the worksheets, I want to adjust the print area according to the value of a cell. I wrote a macro code to do this as below:

Sub Changeprintarea()
If Range("N84").Value <> "" Then<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
PrintArea = "$B$1:$M$87"
ElseIf Range("N84").Value = "" Then<o:p></o:p>
PrintArea = "$B$1:$M$121"<o:p></o:p>
End If<o:p></o:p>
End Sub

This code works Ok, but the problem is I have to click the button every time to make the code run!

Is there a way to make the code run automatically based on the value of the cell ("N84") without clicking button?

Please help
<o:p></o:p>
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "N84" Then
    With Me.PageSetup
        .PrintArea = IIf(Target.Value = "", "$B$1:$M$121", "$B$1:$M$87")
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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