ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error 1004 (https://www.excelbanter.com/excel-programming/399504-error-1004-a.html)

Mike H.

Error 1004
 
I have a sub on a worksheet calculate area and the sub has these 4 lines:

Sheets("Results").Select
Set Rng = Range("nonvalidreport")
ActiveSheet.PageSetup.PrintArea = Rng.Address
ActiveWindow.SelectedSheets.PrintPreview

The sub is not on this sheet. But I get an error 1004 on line 2. If I run
these 4 lines in a module it works, but I can't figure out how to call a sub
in a module from the area where I am. HELP!

George Nicholson

Error 1004
 
Try
Set Rng = Sheets("Results").Range("nonvalidreport")



"Mike H." wrote in message
...
I have a sub on a worksheet calculate area and the sub has these 4 lines:

Sheets("Results").Select
Set Rng = Range("nonvalidreport")
ActiveSheet.PageSetup.PrintArea = Rng.Address
ActiveWindow.SelectedSheets.PrintPreview

The sub is not on this sheet. But I get an error 1004 on line 2. If I
run
these 4 lines in a module it works, but I can't figure out how to call a
sub
in a module from the area where I am. HELP!




Dave Peterson

Error 1004
 
If you're code is in a worksheet module, then those unqualified ranges will
refer to the worksheet that owns the code. And that worksheet may not have a
range named "nonvalidreport".

I'd use this:

with worksheets("Results")
'.select 'not necessary to select
set rng = .range("nonvalidreport")
.pagesetup.printarea = rng.address
.printout preview:=true
end with

or this:

worksheets("results").range("nonvalidreport").prin tout preview:=true

If I didn't really want to change the .pagesetup.printarea, then the one-liner
would be easier to understand (for me).


Mike H. wrote:

I have a sub on a worksheet calculate area and the sub has these 4 lines:

Sheets("Results").Select
Set Rng = Range("nonvalidreport")
ActiveSheet.PageSetup.PrintArea = Rng.Address
ActiveWindow.SelectedSheets.PrintPreview

The sub is not on this sheet. But I get an error 1004 on line 2. If I run
these 4 lines in a module it works, but I can't figure out how to call a sub
in a module from the area where I am. HELP!


--

Dave Peterson


All times are GMT +1. The time now is 06:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com