Error in code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code for Excel 2003:

Private Sub btnPrint1_Click()

Sheets("CapeCod").Select
ActiveSheet.Unprotect
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
ActiveWindow.Visible = False
Windows("FinancialProjections.xls").Activate


' It errors out on this statement
Range("B2").Select


ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Sheets("COSTS").Select

End Sub

I created this code through recording a macro. It is run from a button. Any
ideas on what could be stopping it? The exact error message is....

Run time error 1004
Select method of range class failed.
 
Just a question. Was FinancialProjections.xls openned before you attempted
to activate it?
 
When you have code in the worksheet module, then unqualified ranges will refer
to the worksheet that owns the code.

So you're trying to select B2 of that sheet with the button on it. But since
you're in a different workbook (maybe???) in a different worksheet (maybe???),
your code blows up. (You can only select a range if that sheet is active.)

I don't see why you really want to select B2, but you could try:

Activesheet.range("b2").select
 
Back
Top