setting print area from within a function

SP1938

New Member
Joined
Feb 19, 2002
Messages
1
The following function should set the print area as a function of column number:

---------------------------------------------
Function setprintarea(ByVal column As Integer) As String

' Sets the print area to range "A1:colstring"
Dim colstring As String
colstring = Chr((column Mod 26 + 65)) & "$35" 'last or only char and row number
If Int(column / 26) >= 1 Then colstring = Chr((Int(column / 26) + 64)) & colstring 'higher order
colstring = "$A$1:" & "$" & colstring
setprintarea = colstring 'returns range
Activesheet.PageSetup.printarea = colstring
End Function
---------------------------------------------
The column number is converted to a string and returned for info purposes. The next line should set the print area. This works when used within a sub program locally within VBA, but not from within the function or in the sub from the worksheet. Is there a restriction on these procedures from within functions? The sub name was not found when entered into a cell, thus the function attempt.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You can't change the environment using a function, functions only returns values. You'll have to use a Sub (Or this function and use the result in a Sub)
 
Upvote 0

Forum statistics

Threads
1,213,486
Messages
6,113,932
Members
448,533
Latest member
thietbibeboiwasaco

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