Choosing Range Names in VBA

actjfc

Active Member
Joined
Jun 28, 2003
Messages
412
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!
 

Some videos you may like

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
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
412
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,218
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
412
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,597
Messages
5,597,097
Members
414,123
Latest member
fxledi

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