Choosing Range Names in VBA

actjfc

Active Member
Joined
Jun 28, 2003
Messages
415
Hello Excel Warriors!

I have a workbook with let's say three sheets: A, B, and C. Within them each one has three different named print areas: let's say A1,A2, and A3 - B1, B2, and B3, C1, C2, and C3.

I would like to pass on to a kind of function or sub the variables "Sheet" and "Area", something like:

Sheet = Range("Z1")
Area = Range("Z2")

Sub MacroforPrinting(Sheet,Area)

Active.Sheet = Sheet
PrintArea = Area

With ActiveSheet.PageSetup
.FitToPagesWide = 1
.FitToPagesTall = 1
End With

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

End Sub

Of course the above macro does NOT work. Can somebody help me out?

This sub will be inside a much larger code.

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
What's in Z1 and Z2?
Code:
Sub test()
    Sh = Range("Z1")
    Area = Range("Z2")
    MacroforPrinting Sh, Area
End Sub
Sub MacroforPrinting(Sh, Area)
With Worksheets(Sh)
    With .PageSetup
        .PrintArea = Area
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    .PrintOut Copies:=1, Collate:=True
End With
End Sub
 

actjfc

Active Member
Joined
Jun 28, 2003
Messages
415
Thanks! I doing this:

Dim Area As String
Dim Sh As String

Sub MacroforPrinting()

If Range("Z2") = 3 Then
Sh = "Sheet1"
Area = "Schedule1"
MacroforPrinting Sh, Area
End If

End Sub

Sub MacroforPrinting(Sh, Area)
With Worksheets(Sh)
With .PageSetup
.PrintArea = Area
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
.PrintOut Copies:=1, Collate:=True
End With
End Sub

An error message says "ambigous name detected": MacroforPrinting

How do I fix it? Thanks again!
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
Eh, isn't it quite obvious what the problem is?:)

You've got 2 subs called MacroforPrinting - VBA ain't gonna like that.:eek:
 

actjfc

Active Member
Joined
Jun 28, 2003
Messages
415
I changed it. However, it still does not work. I think the problem is in the definition of the variable "Area". It has a range name in it. Please, somebody help!

Dim Area As String
Dim Sh As String

Sub Test()

If Range("Z10") = 3 Then
Sh = "Sheet1"
Area = "RangeName"
MacroforPrinting Sh, Area
End If

End Sub

Sub MacroforPrinting(Sh, Area)
With Worksheets(Sh)
With .PageSetup
.PrintArea = Area
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
.PrintOut Copies:=1, Collate:=True
End With
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,603
Messages
5,832,668
Members
430,153
Latest member
Javid_P

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
Top